Skip to content

Commit

Permalink
Added AddRouteToContext method (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: Jacob Stuart <[email protected]>
  • Loading branch information
stuartclan and Jacob Stuart authored Apr 24, 2020
1 parent 4a4229d commit ebfe087
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
17 changes: 11 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,19 @@ func AddRouteDataToContext(ctx context.Context, data ContextRouteData) context.C
}

// AddParamsToContext inserts a parameters map into a context using
// the package's internal context key. This function is deprecated.
// Use AddRouteDataToContext instead.
// the package's internal context key.
func AddParamsToContext(ctx context.Context, params map[string]string) context.Context {
data := &contextData{
route: "",
return AddRouteDataToContext(ctx, &contextData{
params: params,
}
return AddRouteDataToContext(ctx, data)
})
}

// AddRouteToContext inserts a route into a context using
// the package's internal context key.
func AddRouteToContext(ctx context.Context, route string) context.Context {
return AddRouteDataToContext(ctx, &contextData{
route: route,
})
}

type contextKey int
Expand Down
54 changes: 54 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)

Expand Down Expand Up @@ -362,3 +363,56 @@ func TestContextMuxSimple(t *testing.T) {
t.Log("Testing with DefaultContext")
router.ServeHTTP(w, r)
}

func TestAddDataToContext(t *testing.T) {
expectedRoute := "/expected/route"
expectedParams := map[string]string{
"test": "expected",
}

ctx := AddRouteDataToContext(context.Background(), &contextData{
route: expectedRoute,
params: expectedParams,
})

if gotData, ok := ctx.Value(contextDataKey).(*contextData); ok && gotData != nil {
if gotData.route != expectedRoute {
t.Errorf("Did not retrieve the desired route. Expected: %s; Got: %s", expectedRoute, gotData.route)
}
if !reflect.DeepEqual(expectedParams, gotData.params) {
t.Errorf("Did not retrieve the desired parameters. Expected: %#v; Got: %#v", expectedParams, gotData.params)
}
} else {
t.Error("failed to retrieve context data")
}
}

func TestAddParamsToContext(t *testing.T) {
expectedParams := map[string]string{
"test": "expected",
}

ctx := AddParamsToContext(context.Background(), expectedParams)

if gotData, ok := ctx.Value(contextDataKey).(*contextData); ok && gotData != nil {
if !reflect.DeepEqual(expectedParams, gotData.params) {
t.Errorf("Did not retrieve the desired parameters. Expected: %#v; Got: %#v", expectedParams, gotData.params)
}
} else {
t.Error("failed to retrieve context data")
}
}

func TestAddRouteToContext(t *testing.T) {
expectedRoute := "/expected/route"

ctx := AddRouteToContext(context.Background(), expectedRoute)

if gotData, ok := ctx.Value(contextDataKey).(*contextData); ok && gotData != nil {
if gotData.route != expectedRoute {
t.Errorf("Did not retrieve the desired route. Expected: %s; Got: %s", expectedRoute, gotData.route)
}
} else {
t.Error("failed to retrieve context data")
}
}

0 comments on commit ebfe087

Please sign in to comment.