Skip to content

Commit

Permalink
Fix router path parameter and add regression tests. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Davis authored Oct 23, 2019
1 parent 445c9d5 commit b231155
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/api/middleware/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (ctx *CachedOptlyMiddleware) ClientCtx(next http.Handler) http.Handler {

sdkKey := r.Header.Get(OptlySDKHeader)

GetLogger(r).Info().Msg("Fetching new OptimizelyClient")
GetLogger(r).Info().Msg("Fetching OptimizelyClient")
var err error
var optlyClient *optimizely.OptlyClient
if sdkKey == "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewRouter(opt *RouterOptions) *chi.Mux {
r.Get("/{featureKey}", opt.featureAPI.GetFeature)
})

r.Route("/users/{userId}", func(r chi.Router) {
r.Route("/users/{userID}", func(r chi.Router) {
r.Use(opt.middleware.ClientCtx, opt.middleware.UserCtx)
// TODO r.Get("/features/{featureKey}", opt.userAPI.ActivateFeature)
r.Post("/features/{featureKey}", opt.userAPI.ActivateFeature)
Expand Down
74 changes: 65 additions & 9 deletions pkg/api/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
package api

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/optimizely/sidedoor/pkg/optimizelytest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

Expand All @@ -49,18 +52,41 @@ func (m *MockOptlyMiddleware) UserCtx(next http.Handler) http.Handler {

type MockFeatureAPI struct{}

func (m *MockFeatureAPI) ListFeatures(w http.ResponseWriter, r *http.Request) {}
func (m *MockFeatureAPI) GetFeature(w http.ResponseWriter, r *http.Request) {}
func (m *MockFeatureAPI) ListFeatures(w http.ResponseWriter, r *http.Request) {
renderPathParams(w, r)
}
func (m *MockFeatureAPI) GetFeature(w http.ResponseWriter, r *http.Request) {
renderPathParams(w, r)
}

type MockUserEventAPI struct{}

func (m *MockUserEventAPI) AddUserEvent(w http.ResponseWriter, r *http.Request) {}
func (m *MockUserEventAPI) AddUserEvent(w http.ResponseWriter, r *http.Request) {
renderPathParams(w, r)
}

type MockUserAPI struct{}

func (m *MockUserAPI) TrackEvent(w http.ResponseWriter, r *http.Request) {}
func (m *MockUserAPI) TrackEvent(w http.ResponseWriter, r *http.Request) {
renderPathParams(w, r)
}

func (m *MockUserAPI) ActivateFeature(w http.ResponseWriter, r *http.Request) {
renderPathParams(w, r)
}

func (m *MockUserAPI) ActivateFeature(w http.ResponseWriter, r *http.Request) {}
func renderPathParams(w http.ResponseWriter, r *http.Request) {
pathParams := make(map[string]string)
rctx := chi.RouteContext(r.Context())
for i, k := range rctx.URLParams.Keys {
if k == "*" {
continue
}
pathParams[k] = rctx.URLParams.Values[i]
}

render.JSON(w, r, pathParams)
}

type RouterTestSuite struct {
suite.Suite
Expand All @@ -86,36 +112,66 @@ func (suite *RouterTestSuite) TestListFeatures() {
req := httptest.NewRequest("GET", "/features", nil)
rec := httptest.NewRecorder()
suite.mux.ServeHTTP(rec, req)
suite.Equal(http.StatusOK, rec.Code)

suite.Equal("expected", rec.Header().Get(clientHeaderKey))
suite.Empty(rec.Header().Get(userHeaderKey))

expected := map[string]string{}
suite.assertValid(rec, expected)
}

func (suite *RouterTestSuite) TestGetFeature() {
req := httptest.NewRequest("GET", "/features/one", nil)
rec := httptest.NewRecorder()
suite.mux.ServeHTTP(rec, req)
suite.Equal(http.StatusOK, rec.Code)

suite.Equal("expected", rec.Header().Get(clientHeaderKey))
suite.Empty(rec.Header().Get(userHeaderKey))

expected := map[string]string{
"featureKey": "one",
}
suite.assertValid(rec, expected)
}

func (suite *RouterTestSuite) TestActivateFeatures() {
req := httptest.NewRequest("POST", "/users/me/features/one", nil)
rec := httptest.NewRecorder()
suite.mux.ServeHTTP(rec, req)
suite.Equal(http.StatusOK, rec.Code)

suite.Equal("expected", rec.Header().Get(clientHeaderKey))
suite.Equal("expected", rec.Header().Get(userHeaderKey))

expected := map[string]string{
"userID": "me",
"featureKey": "one",
}
suite.assertValid(rec, expected)
}

func (suite *RouterTestSuite) TestTrackEvent() {
req := httptest.NewRequest("POST", "/users/me/events/key", nil)
rec := httptest.NewRecorder()
suite.mux.ServeHTTP(rec, req)
suite.Equal(http.StatusOK, rec.Code)

suite.Equal("expected", rec.Header().Get(clientHeaderKey))
suite.Equal("expected", rec.Header().Get(userHeaderKey))

expected := map[string]string{
"userID": "me",
"eventKey": "key",
}
suite.assertValid(rec, expected)
}

func (suite *RouterTestSuite) assertValid(rec *httptest.ResponseRecorder, expected map[string]string) {
suite.Equal(http.StatusOK, rec.Code)

var actual map[string]string
err := json.Unmarshal(rec.Body.Bytes(), &actual)
assert.NoError(suite.T(), err)

assert.Equal(suite.T(), expected, actual)
}

func TestRouter(t *testing.T) {
Expand Down

0 comments on commit b231155

Please sign in to comment.