-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathinterop_handler_test.go
127 lines (108 loc) · 3.86 KB
/
interop_handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//go:build small
// +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 that the path has been properly updated to the current interop effort.
assert.Equal(t, loc.Path, "/interop-2024")
// Check if embedded param is maintained after redirect.
assert.Equal(t, loc.RawQuery, "embedded")
}
func TestInteropHandler_redirectMobile(t *testing.T) {
// 2021 is an invalid interop mobile year and should be redirected.
req := httptest.NewRequest("GET", "/interop-2021?mobile-view", strings.NewReader("{}"))
req = mux.SetURLVars(req, map[string]string{
"name": "interop",
"year": "2021",
"mobileView": "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 that the path has been properly updated to the current interop effort.
assert.Equal(t, loc.Path, "/interop-2024")
// Check if mobileView param is maintained after redirect.
assert.Equal(t, loc.RawQuery, "mobile-view")
}
func TestInteropHandler_redirectdefault(t *testing.T) {
// /interop route should redirect to the current default interop year dashboard.
req := httptest.NewRequest("GET", "/interop?embedded", strings.NewReader("{}"))
req = mux.SetURLVars(req, map[string]string{
"name": "interop",
"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 that the path has been properly updated to the current interop effort.
assert.Equal(t, loc.Path, "/interop-2024")
// Check if embedded param is maintained after redirect.
assert.Equal(t, loc.RawQuery, "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)
}
func TestInteropHandler_mobileSuccess(t *testing.T) {
// A typical "/interop-20XX" path with a valid mobile year should not redirect.
req := httptest.NewRequest(
"GET", "/interop-"+defaultRedirectYear+"?mobile-view", strings.NewReader("{}"))
req = mux.SetURLVars(req, map[string]string{
"name": "interop",
"year": defaultRedirectYear,
"mobileView": "true",
})
w := httptest.NewRecorder()
interopHandler(w, req)
resp := w.Result()
assert.Equal(t, resp.StatusCode, http.StatusOK)
}