golang.org/x/build@v0.0.0-20240506185731-218518f32b70/devapp/server_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"crypto/tls"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"testing"
    12  )
    13  
    14  var testServer = newServer(http.DefaultServeMux, "./static/", "./templates/", false)
    15  
    16  func TestStaticAssetsFound(t *testing.T) {
    17  	req := httptest.NewRequest("GET", "/", nil)
    18  	w := httptest.NewRecorder()
    19  	testServer.ServeHTTP(w, req)
    20  	if w.Code != http.StatusOK {
    21  		t.Errorf("expected code %d, got %d", http.StatusOK, w.Code)
    22  	}
    23  	if hdr := w.Header().Get("Content-Type"); hdr != "text/html; charset=utf-8" {
    24  		t.Errorf("incorrect Content-Type header, got headers: %v", w.Header())
    25  	}
    26  }
    27  
    28  func TestFaviconFound(t *testing.T) {
    29  	req := httptest.NewRequest("GET", "/favicon.ico", nil)
    30  	w := httptest.NewRecorder()
    31  	testServer.ServeHTTP(w, req)
    32  	if w.Code != http.StatusOK {
    33  		t.Errorf("expected code %d, got %d", http.StatusOK, w.Code)
    34  	}
    35  	if hdr := w.Header().Get("Content-Type"); hdr != "image/x-icon" {
    36  		t.Errorf("incorrect Content-Type header, got headers: %v", w.Header())
    37  	}
    38  }
    39  
    40  func TestHSTSHeaderSet(t *testing.T) {
    41  	req := httptest.NewRequest("GET", "/", nil)
    42  	req.TLS = &tls.ConnectionState{}
    43  	w := httptest.NewRecorder()
    44  	testServer.ServeHTTP(w, req)
    45  	if hdr := w.Header().Get("Strict-Transport-Security"); hdr == "" {
    46  		t.Errorf("missing Strict-Transport-Security header; headers = %v", w.Header())
    47  	}
    48  }
    49  
    50  func TestRandomHelpWantedIssue(t *testing.T) {
    51  	req := httptest.NewRequest("GET", "/imfeelinglucky", nil)
    52  	w := httptest.NewRecorder()
    53  	testServer.ServeHTTP(w, req)
    54  	if w.Code != http.StatusSeeOther {
    55  		t.Errorf("w.Code = %d; want %d", w.Code, http.StatusSeeOther)
    56  	}
    57  	if g, w := w.Header().Get("Location"), issuesURLBase; g != w {
    58  		t.Errorf("Location header = %q; want %q", g, w)
    59  	}
    60  
    61  	testServer.cMu.Lock()
    62  	testServer.helpWantedIssues = []issueData{{id: 42}}
    63  	testServer.cMu.Unlock()
    64  	w = httptest.NewRecorder()
    65  	testServer.ServeHTTP(w, req)
    66  	if w.Code != http.StatusSeeOther {
    67  		t.Errorf("w.Code = %d; want %d", w.Code, http.StatusSeeOther)
    68  	}
    69  	if g, w := w.Header().Get("Location"), issuesURLBase+"42"; g != w {
    70  		t.Errorf("Location header = %q; want %q", g, w)
    71  	}
    72  }
    73  
    74  func TestRandomFilteredHelpWantedIssue(t *testing.T) {
    75  	req := httptest.NewRequest("GET", "/imfeelinglucky?pkg=foo", nil)
    76  	w := httptest.NewRecorder()
    77  	testServer.ServeHTTP(w, req)
    78  	if w.Code != http.StatusSeeOther {
    79  		t.Errorf("w.Code = %d; want %d", w.Code, http.StatusSeeOther)
    80  	}
    81  	if g, w := w.Header().Get("Location"), issuesURLBase; g != w {
    82  		t.Errorf("Location header = %q; want %q", g, w)
    83  	}
    84  
    85  	testServer.cMu.Lock()
    86  	testServer.helpWantedIssues = []issueData{
    87  		{id: 41, titlePrefix: "not/foo: bar"},
    88  		{id: 42, titlePrefix: "foo/bar: baz"},
    89  	}
    90  	testServer.cMu.Unlock()
    91  	w = httptest.NewRecorder()
    92  	testServer.ServeHTTP(w, req)
    93  	if w.Code != http.StatusSeeOther {
    94  		t.Errorf("w.Code = %d; want %d", w.Code, http.StatusSeeOther)
    95  	}
    96  	if g, w := w.Header().Get("Location"), issuesURLBase+"42"; g != w {
    97  		t.Errorf("Location header = %q; want %q", g, w)
    98  	}
    99  }
   100  
   101  func TestHandleDirRedirect(t *testing.T) {
   102  	tests := []struct {
   103  		path string
   104  		ref  string
   105  		want string
   106  	}{
   107  		{"/dir/build", "", "https://github.com/golang/build/tree/master/"},
   108  		{"/dir/build/", "", "https://github.com/golang/build/tree/master/"},
   109  		{"/dir/build", "https://go.googlesource.com/", "https://go.googlesource.com/build/+/master/"},
   110  		{"/dir/build/", "https://go.googlesource.com/", "https://go.googlesource.com/build/+/master/"},
   111  		{"/dir/build/maintner", "", "https://github.com/golang/build/tree/master/maintner"},
   112  		{"/dir/build/maintner/", "", "https://github.com/golang/build/tree/master/maintner"},
   113  		{"/dir/build/maintner", "https://go.googlesource.com/", "https://go.googlesource.com/build/+/master/maintner"},
   114  		{"/dir/build/maintner/", "https://go.googlesource.com/", "https://go.googlesource.com/build/+/master/maintner"},
   115  	}
   116  	for _, tt := range tests {
   117  		req := httptest.NewRequest("GET", tt.path, nil)
   118  		if tt.ref != "" {
   119  			req.Header.Set("Referer", tt.ref)
   120  		}
   121  		w := httptest.NewRecorder()
   122  		testServer.ServeHTTP(w, req)
   123  		if w.Code != http.StatusFound {
   124  			t.Errorf("for %q from %q, got code %d, want %d", tt.path, tt.ref, w.Code, http.StatusFound)
   125  			continue
   126  		}
   127  		got := w.Header().Get("Location")
   128  		if got != tt.want {
   129  			t.Errorf("for %q from %q, got %q; want %q", tt.path, tt.ref, got, tt.want)
   130  		}
   131  	}
   132  }