Skip to content

Commit

Permalink
Reusable Interop 20XX page (#2944)
Browse files Browse the repository at this point in the history
* 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 <kyleju@google.com>

* remove unused variable

* Reference all year info from the data manager

Co-authored-by: Kyle Ju <kyleju@google.com>
  • Loading branch information
DanielRyanSmith and KyleJu committed Sep 28, 2022
1 parent 859f043 commit 9c7b693
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 347 deletions.
34 changes: 0 additions & 34 deletions webapp/compat_2021_handler.go

This file was deleted.

548 changes: 277 additions & 271 deletions webapp/components/interop-2022.js → webapp/components/interop.js

Large diffs are not rendered by default.

35 changes: 0 additions & 35 deletions webapp/interop_2022_handler.go

This file was deleted.

64 changes: 64 additions & 0 deletions webapp/interop_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package webapp

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/web-platform-tests/wpt.fyi/shared"
)

type interopData struct {
Embedded bool
Year string
}

// Set of years that are valid for Interop 20XX.
var validYears = map[string]bool{"2021": true, "2022": true}

// Year that any invalid year will redirect to.
const defaultRedirectYear = "2022"

// interopHandler handles GET requests to /interop-20XX and /compat20XX
func interopHandler(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
year := mux.Vars(r)["year"]

// /compat20XX redirects to /interop-20XX
needsRedirect := name == "compat"
// TODO(danielrsmith): Change this redirect for next year's interop page.
if _, ok := validYears[year]; !ok {
year = defaultRedirectYear
needsRedirect = true
}

if needsRedirect {
destination := *(r.URL)

destination.Path = fmt.Sprintf("interop-%s", year)
http.Redirect(w, r, destination.String(), http.StatusTemporaryRedirect)
return
}

if r.Method != "GET" {
http.Error(w, "Only GET is supported.", http.StatusMethodNotAllowed)
return
}

q := r.URL.Query()
embedded, err := shared.ParseBooleanParam(q, "embedded")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

data := interopData{
Embedded: embedded != nil && *embedded,
Year: year,
}
RenderTemplate(w, r, "interop.html", data)
}
65 changes: 65 additions & 0 deletions webapp/interop_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// +build small

package webapp

// Copyright 2022 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)

func TestInteropHandler_redirect(t *testing.T) {
// 1999 is an invalid interop year and should be redirected.
req := httptest.NewRequest("GET", "/interop-1999?embedded", strings.NewReader("{}"))
req = mux.SetURLVars(req, map[string]string{
"name": "interop",
"year": "1999",
"embedded": "true",
})

w := httptest.NewRecorder()
interopHandler(w, req)
resp := w.Result()
assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)

loc, err := resp.Location()
assert.Nil(t, err)
// Check if embedded param is maintained after redirect.
assert.NotEqual(t, loc.Path, "interop-2022?embedded")
}

func TestInteropHandler_compatRedirect(t *testing.T) {
// "/compat20XX" paths should redirect to the interop version of the given year.
req := httptest.NewRequest("GET", "/compat2021", strings.NewReader("{}"))
req = mux.SetURLVars(req, map[string]string{
"name": "compat",
"year": "2021",
})

w := httptest.NewRecorder()
interopHandler(w, req)
resp := w.Result()
assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
}

func TestInteropHandler_success(t *testing.T) {
// A typical "/interop-20XX" path with a valid year should not redirect.
req := httptest.NewRequest("GET", "/interop-"+defaultRedirectYear, strings.NewReader("{}"))
req = mux.SetURLVars(req, map[string]string{
"name": "interop",
"year": defaultRedirectYear,
})

w := httptest.NewRecorder()
interopHandler(w, req)
resp := w.Result()
assert.Equal(t, resp.StatusCode, http.StatusOK)
}
7 changes: 2 additions & 5 deletions webapp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ func RegisterRoutes() {
shared.AddRoute("/runs", "test-runs", testRunsHandler)
shared.AddRoute("/test-runs", "test-runs", testRunsHandler) // Legacy name

// Dashboard for the compat-2021 effort.
shared.AddRoute("/compat2021", "compat-2021", compat2021Handler)

// Dashboard for the interop-2022 effort.
shared.AddRoute("/interop-2022", "interop-2022", interop2022Handler)
// Dashboard for the interop effort, by year.
shared.AddRoute("/{name:(?:compat|interop-)}{year:[0-9]+}", "interop-dashboard", interopHandler)

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

0 comments on commit 9c7b693

Please sign in to comment.