github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/webapp/interop_handler_test.go (about)

     1  //go:build small
     2  // +build small
     3  
     4  package webapp
     5  
     6  // Copyright 2022 The WPT Dashboard Project. All rights reserved.
     7  // Use of this source code is governed by a BSD-style license that can be
     8  // found in the LICENSE file.
     9  
    10  import (
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/gorilla/mux"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestInteropHandler_redirect(t *testing.T) {
    21  	// 1999 is an invalid interop year and should be redirected.
    22  	req := httptest.NewRequest("GET", "/interop-1999?embedded", strings.NewReader("{}"))
    23  	req = mux.SetURLVars(req, map[string]string{
    24  		"name":     "interop",
    25  		"year":     "1999",
    26  		"embedded": "true",
    27  	})
    28  
    29  	w := httptest.NewRecorder()
    30  	interopHandler(w, req)
    31  	resp := w.Result()
    32  	assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
    33  
    34  	loc, err := resp.Location()
    35  	assert.Nil(t, err)
    36  	// Check that the path has been properly updated to the current interop effort.
    37  	assert.Equal(t, loc.Path, "/interop-2024")
    38  	// Check if embedded param is maintained after redirect.
    39  	assert.Equal(t, loc.RawQuery, "embedded")
    40  }
    41  
    42  func TestInteropHandler_redirectdefault(t *testing.T) {
    43  	// /interop route should redirect to the current default interop year dashboard.
    44  	req := httptest.NewRequest("GET", "/interop?embedded", strings.NewReader("{}"))
    45  	req = mux.SetURLVars(req, map[string]string{
    46  		"name":     "interop",
    47  		"embedded": "true",
    48  	})
    49  
    50  	w := httptest.NewRecorder()
    51  	interopHandler(w, req)
    52  	resp := w.Result()
    53  	assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
    54  
    55  	loc, err := resp.Location()
    56  	assert.Nil(t, err)
    57  	// Check that the path has been properly updated to the current interop effort.
    58  	assert.Equal(t, loc.Path, "/interop-2024")
    59  	// Check if embedded param is maintained after redirect.
    60  	assert.Equal(t, loc.RawQuery, "embedded")
    61  }
    62  
    63  func TestInteropHandler_compatRedirect(t *testing.T) {
    64  	// "/compat20XX" paths should redirect to the interop version of the given year.
    65  	req := httptest.NewRequest("GET", "/compat2021", strings.NewReader("{}"))
    66  	req = mux.SetURLVars(req, map[string]string{
    67  		"name": "compat",
    68  		"year": "2021",
    69  	})
    70  
    71  	w := httptest.NewRecorder()
    72  	interopHandler(w, req)
    73  	resp := w.Result()
    74  	assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
    75  }
    76  
    77  func TestInteropHandler_success(t *testing.T) {
    78  	// A typical "/interop-20XX" path with a valid year should not redirect.
    79  	req := httptest.NewRequest("GET", "/interop-"+defaultRedirectYear, strings.NewReader("{}"))
    80  	req = mux.SetURLVars(req, map[string]string{
    81  		"name": "interop",
    82  		"year": defaultRedirectYear,
    83  	})
    84  
    85  	w := httptest.NewRecorder()
    86  	interopHandler(w, req)
    87  	resp := w.Result()
    88  	assert.Equal(t, resp.StatusCode, http.StatusOK)
    89  }