Skip to content

Commit fa748f2

Browse files
authored
add support for v2 assignment overrides endpoint in fake server (#57)
* add support for v2 assignment overrides endpoint in fake server * add test for new version of assignment overrides * bump version to 1.1.3
1 parent a5a3335 commit fa748f2

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
SHELL = /bin/sh
22

3-
VERSION=1.1.2
3+
VERSION=1.1.3
44
BUILD=`git rev-parse HEAD`
55

66
LDFLAGS=-ldflags "-w -s \

fakeserver/routes.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ type v1Assignment struct {
2626
Unsynced bool `json:"unsynced"`
2727
}
2828

29+
// v2AssignmentOverrideRequestBody is the JSON input for the V2 assignment override endpoint
30+
type v2AssignmentOverrideRequestBody struct {
31+
Assignments []v1Assignment `json:"assignments"`
32+
}
33+
2934
// v1VisitorConfig is the JSON output type for V1 visitor_config endpoints
3035
type v1VisitorConfig struct {
3136
Splits map[string]*splits.Weights `json:"splits"`
@@ -112,6 +117,10 @@ func (s *server) routes() {
112117
"/api/v1/assignment_override",
113118
postV1AssignmentOverride,
114119
)
120+
s.handlePostReturnNoContent(
121+
"/api/v2/visitors/{v}/assignment_overrides",
122+
postV2AssignmentOverride,
123+
)
115124
s.handleGet(
116125
"/api/v1/apps/{a}/versions/{v}/builds/{b}/visitors/{id}/config",
117126
getV1AppVisitorConfig,
@@ -257,6 +266,35 @@ func postV1AssignmentOverride(r *http.Request) error {
257266
return nil
258267
}
259268

269+
func postV2AssignmentOverride(r *http.Request) error {
270+
var assignments []v1Assignment
271+
contentType := r.Header.Get("content-type")
272+
switch {
273+
case strings.HasPrefix(contentType, "application/json"):
274+
requestBytes, err := ioutil.ReadAll(r.Body)
275+
if err != nil {
276+
return err
277+
}
278+
var assignmentBody v2AssignmentOverrideRequestBody
279+
err = json.Unmarshal(requestBytes, &assignmentBody)
280+
if err != nil {
281+
return err
282+
}
283+
assignments = assignmentBody.Assignments
284+
default:
285+
return fmt.Errorf("got unexpected content type %s", contentType)
286+
}
287+
storedAssignments, err := fakeassignments.Read()
288+
for _, assignment := range assignments {
289+
(*storedAssignments)[assignment.SplitName] = assignment.Variant
290+
}
291+
err = fakeassignments.Write(storedAssignments)
292+
if err != nil {
293+
return err
294+
}
295+
return nil
296+
}
297+
260298
func getV1AppVisitorConfig() (interface{}, error) {
261299
isplitRegistry, err := getV1SplitRegistry()
262300
splitRegistry := isplitRegistry.(map[string]*splits.Weights)

fakeserver/server_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fakeserver
22

33
import (
4+
"bytes"
45
"io/ioutil"
56
"log"
67
"net/http"
@@ -27,6 +28,10 @@ splits:
2728
weights:
2829
control: 60
2930
treatment: 40
31+
- name: test.test2_experiment
32+
weights:
33+
control: 60
34+
treatment: 40
3035
`
3136

3237
func TestMain(m *testing.M) {
@@ -150,3 +155,40 @@ func TestPersistAssignment(t *testing.T) {
150155
require.Equal(t, "control", (*assignments)["test.test_experiment"])
151156
})
152157
}
158+
159+
func TestPersistAssignmentV2(t *testing.T) {
160+
os.Remove("testdata/assignments.yml")
161+
162+
t.Run("it persists assignments to yaml", func(t *testing.T) {
163+
w := httptest.NewRecorder()
164+
h := createHandler()
165+
166+
overrides := v2AssignmentOverrideRequestBody{
167+
Assignments: []v1Assignment{
168+
v1Assignment{
169+
SplitName: "test.test_experiment",
170+
Variant: "control",
171+
},
172+
v1Assignment{
173+
SplitName: "test.test2_experiment",
174+
Variant: "treatment",
175+
},
176+
},
177+
}
178+
data, err := json.Marshal(overrides)
179+
require.Nil(t, err)
180+
181+
request := httptest.NewRequest("POST", "/api/v2/visitors/1/assignment_overrides", bytes.NewReader(data))
182+
request.Header.Add("Content-Type", "application/json")
183+
request.Header.Add("Content-Length", strconv.Itoa(len(data)))
184+
185+
h.ServeHTTP(w, request)
186+
187+
require.Equal(t, http.StatusNoContent, w.Code)
188+
189+
assignments, err := fakeassignments.Read()
190+
require.Nil(t, err)
191+
require.Equal(t, "control", (*assignments)["test.test_experiment"])
192+
require.Equal(t, "treatment", (*assignments)["test.test2_experiment"])
193+
})
194+
}

0 commit comments

Comments
 (0)