Skip to content

Commit 9c7b693

Browse files
Reusable Interop 20XX page (#2944)
* year-agnostic interop pages * interchangeable prose and issue URL * lint * eslint changes * last linting update * add date limiter * typo * 2021 uses same csv parser * comments * change view by year * redirect compat2021 to interop-2021 * redirect for invalid years * Add tests for interop handler * changes suggested by @jcscottiii * Update webapp/components/interop.js Co-authored-by: Kyle Ju <[email protected]> * remove unused variable * Reference all year info from the data manager Co-authored-by: Kyle Ju <[email protected]>
1 parent 859f043 commit 9c7b693

File tree

8 files changed

+604
-347
lines changed

8 files changed

+604
-347
lines changed

webapp/compat_2021_handler.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

webapp/components/interop-2022.js renamed to webapp/components/interop.js

Lines changed: 277 additions & 271 deletions
Large diffs are not rendered by default.

webapp/interop_2022_handler.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

webapp/interop_handler.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2022 The WPT Dashboard Project. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package webapp
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
11+
"github.com/gorilla/mux"
12+
"github.com/web-platform-tests/wpt.fyi/shared"
13+
)
14+
15+
type interopData struct {
16+
Embedded bool
17+
Year string
18+
}
19+
20+
// Set of years that are valid for Interop 20XX.
21+
var validYears = map[string]bool{"2021": true, "2022": true}
22+
23+
// Year that any invalid year will redirect to.
24+
const defaultRedirectYear = "2022"
25+
26+
// interopHandler handles GET requests to /interop-20XX and /compat20XX
27+
func interopHandler(w http.ResponseWriter, r *http.Request) {
28+
name := mux.Vars(r)["name"]
29+
year := mux.Vars(r)["year"]
30+
31+
// /compat20XX redirects to /interop-20XX
32+
needsRedirect := name == "compat"
33+
// TODO(danielrsmith): Change this redirect for next year's interop page.
34+
if _, ok := validYears[year]; !ok {
35+
year = defaultRedirectYear
36+
needsRedirect = true
37+
}
38+
39+
if needsRedirect {
40+
destination := *(r.URL)
41+
42+
destination.Path = fmt.Sprintf("interop-%s", year)
43+
http.Redirect(w, r, destination.String(), http.StatusTemporaryRedirect)
44+
return
45+
}
46+
47+
if r.Method != "GET" {
48+
http.Error(w, "Only GET is supported.", http.StatusMethodNotAllowed)
49+
return
50+
}
51+
52+
q := r.URL.Query()
53+
embedded, err := shared.ParseBooleanParam(q, "embedded")
54+
if err != nil {
55+
http.Error(w, err.Error(), http.StatusBadRequest)
56+
return
57+
}
58+
59+
data := interopData{
60+
Embedded: embedded != nil && *embedded,
61+
Year: year,
62+
}
63+
RenderTemplate(w, r, "interop.html", data)
64+
}

webapp/interop_handler_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// +build small
2+
3+
package webapp
4+
5+
// Copyright 2022 The WPT Dashboard Project. All rights reserved.
6+
// Use of this source code is governed by a BSD-style license that can be
7+
// found in the LICENSE file.
8+
9+
import (
10+
"net/http"
11+
"net/http/httptest"
12+
"strings"
13+
"testing"
14+
15+
"github.com/gorilla/mux"
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestInteropHandler_redirect(t *testing.T) {
20+
// 1999 is an invalid interop year and should be redirected.
21+
req := httptest.NewRequest("GET", "/interop-1999?embedded", strings.NewReader("{}"))
22+
req = mux.SetURLVars(req, map[string]string{
23+
"name": "interop",
24+
"year": "1999",
25+
"embedded": "true",
26+
})
27+
28+
w := httptest.NewRecorder()
29+
interopHandler(w, req)
30+
resp := w.Result()
31+
assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
32+
33+
loc, err := resp.Location()
34+
assert.Nil(t, err)
35+
// Check if embedded param is maintained after redirect.
36+
assert.NotEqual(t, loc.Path, "interop-2022?embedded")
37+
}
38+
39+
func TestInteropHandler_compatRedirect(t *testing.T) {
40+
// "/compat20XX" paths should redirect to the interop version of the given year.
41+
req := httptest.NewRequest("GET", "/compat2021", strings.NewReader("{}"))
42+
req = mux.SetURLVars(req, map[string]string{
43+
"name": "compat",
44+
"year": "2021",
45+
})
46+
47+
w := httptest.NewRecorder()
48+
interopHandler(w, req)
49+
resp := w.Result()
50+
assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
51+
}
52+
53+
func TestInteropHandler_success(t *testing.T) {
54+
// A typical "/interop-20XX" path with a valid year should not redirect.
55+
req := httptest.NewRequest("GET", "/interop-"+defaultRedirectYear, strings.NewReader("{}"))
56+
req = mux.SetURLVars(req, map[string]string{
57+
"name": "interop",
58+
"year": defaultRedirectYear,
59+
})
60+
61+
w := httptest.NewRecorder()
62+
interopHandler(w, req)
63+
resp := w.Result()
64+
assert.Equal(t, resp.StatusCode, http.StatusOK)
65+
}

webapp/routes.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ func RegisterRoutes() {
3737
shared.AddRoute("/runs", "test-runs", testRunsHandler)
3838
shared.AddRoute("/test-runs", "test-runs", testRunsHandler) // Legacy name
3939

40-
// Dashboard for the compat-2021 effort.
41-
shared.AddRoute("/compat2021", "compat-2021", compat2021Handler)
42-
43-
// Dashboard for the interop-2022 effort.
44-
shared.AddRoute("/interop-2022", "interop-2022", interop2022Handler)
40+
// Dashboard for the interop effort, by year.
41+
shared.AddRoute("/{name:(?:compat|interop-)}{year:[0-9]+}", "interop-dashboard", interopHandler)
4542

4643
// Admin-only manual results upload.
4744
shared.AddRoute("/admin/results/upload", "admin-results-upload", adminUploadHandler)

0 commit comments

Comments
 (0)