internal/middleware: change experiment query param behavior

Previously, setting an experiment with the experiment query param
required the flag to be present in the experiments table.

Feature flags can now be set with the experiment query param without
first being added to the database.

Change-Id: Ia24862f2e05414547b5f2dfb96561af49b568835
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/760279
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Andrew Bonventre <andybons@google.com>
This commit is contained in:
Julie Qiu 2020-06-02 13:25:29 -04:00 коммит произвёл Julie Qiu
Родитель bd43c94d8d
Коммит 375894fde3
2 изменённых файлов: 4 добавлений и 29 удалений

Просмотреть файл

@ -69,6 +69,10 @@ func (e *Experimenter) setExperimentsForRequest(r *http.Request) *http.Request {
set[exp.Name] = true
}
}
keys := r.URL.Query()[experimentQueryParamKey]
for _, k := range keys {
set[k] = true
}
return r.WithContext(experiment.NewContext(r.Context(), experiment.NewSet(set)))
}
@ -113,12 +117,6 @@ func (e *Experimenter) loadNextSnapshot(ctx context.Context) (err error) {
// All requests from the same IP will be enrolled in the same set of
// experiments.
func shouldSetExperiment(r *http.Request, e *internal.Experiment) bool {
keys := r.URL.Query()[experimentQueryParamKey]
for _, k := range keys {
if k == e.Name {
return true
}
}
if e.Rollout == 0 {
return false
}

Просмотреть файл

@ -137,26 +137,3 @@ func TestShouldSetExperiment(t *testing.T) {
})
}
}
func TestShouldSetExperimentWithQueryParam(t *testing.T) {
req, err := http.NewRequest("GET", "http://foo", nil)
if err != nil {
t.Fatal(err)
}
testExperiments := []string{
"experiment-test-1",
"experiment-test-2",
}
q := req.URL.Query()
for _, te := range testExperiments {
q.Add(experimentQueryParamKey, te)
q.Add(experimentQueryParamKey, te)
}
req.URL.RawQuery = q.Encode()
for _, te := range testExperiments {
if !shouldSetExperiment(req, &internal.Experiment{Name: te, Rollout: 0}) {
t.Errorf("shouldSetExperiment(%q) = false; want = true", te)
}
}
}