github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/labels_medium_test.go (about) 1 //go:build medium 2 // +build medium 3 4 // Copyright 2019 The WPT Dashboard Project. All rights reserved. 5 // Use of this source code is governed by a BSD-style license that can be 6 // found in the LICENSE file. 7 8 package api 9 10 import ( 11 "encoding/json" 12 "io" 13 "net/http" 14 "net/http/httptest" 15 "testing" 16 17 "github.com/stretchr/testify/assert" 18 "github.com/web-platform-tests/wpt.fyi/shared" 19 "github.com/web-platform-tests/wpt.fyi/shared/sharedtest" 20 ) 21 22 func TestLabelsHandler(t *testing.T) { 23 ctx, done, err := sharedtest.NewAEContext(true) 24 assert.Nil(t, err) 25 defer done() 26 27 store := shared.NewAppEngineDatastore(ctx, false) 28 key := store.NewIncompleteKey("TestRun") 29 _, err = store.Put(key, &shared.TestRun{Labels: []string{"b", "c"}}) 30 _, err = store.Put(key, &shared.TestRun{Labels: []string{"b", "a"}}) 31 assert.Nil(t, err) 32 33 handler := LabelsHandler{ctx: ctx} 34 w := httptest.NewRecorder() 35 r := httptest.NewRequest("GET", "/api/labels", nil) 36 handler.ServeHTTP(w, r) 37 labels := parseLabelsResponse(t, w) 38 assert.Equal(t, labels, []string{"a", "b", "c"}) // Ordered and deduped 39 } 40 41 func parseLabelsResponse(t *testing.T, w *httptest.ResponseRecorder) []string { 42 assert.Equal(t, http.StatusOK, w.Result().StatusCode) 43 out, _ := io.ReadAll(w.Body) 44 var labels []string 45 json.Unmarshal(out, &labels) 46 return labels 47 }