github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safehttp/handler_test.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package safehttp_test
    16  
    17  import (
    18  	"net/http/httptest"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/go-safeweb/safehttp"
    24  	"github.com/google/safehtml"
    25  )
    26  
    27  var BarHandler = safehttp.HandlerFunc(func(w safehttp.ResponseWriter, r *safehttp.IncomingRequest) safehttp.Result {
    28  	if !strings.HasPrefix(r.URL().Path(), "/bar") {
    29  		return w.WriteError(safehttp.StatusBadRequest)
    30  	}
    31  	return w.Write(safehtml.HTMLEscaped("Hello!"))
    32  })
    33  
    34  func TestStripPrefix(t *testing.T) {
    35  	mux := safehttp.NewServeMuxConfig(nil).Mux()
    36  
    37  	mux.Handle("/bar", safehttp.MethodGet, BarHandler)
    38  	mux.Handle("/more/bar", safehttp.MethodGet, safehttp.StripPrefix("/more", BarHandler))
    39  
    40  	r := httptest.NewRequest(safehttp.MethodGet, "http://foo.com/bar", nil)
    41  	rw := httptest.NewRecorder()
    42  	mux.ServeHTTP(rw, r)
    43  
    44  	rStrip := httptest.NewRequest(safehttp.MethodGet, "http://foo.com/more/bar", nil)
    45  	rwStrip := httptest.NewRecorder()
    46  	mux.ServeHTTP(rwStrip, rStrip)
    47  
    48  	if rwStrip.Code != rw.Code {
    49  		t.Errorf("Code got %v, want %v", rwStrip.Code, rw.Code)
    50  	}
    51  
    52  	if diff := cmp.Diff(rw.Header(), rwStrip.Header()); diff != "" {
    53  		t.Errorf("Header() mismatch (-want +got):\n%s", diff)
    54  	}
    55  
    56  	if got := rwStrip.Body.String(); got != rw.Body.String() {
    57  		t.Errorf("response body: got %q want %q", got, rw.Body.String())
    58  	}
    59  }
    60  
    61  func TestStripPrefixPanic(t *testing.T) {
    62  	mux := safehttp.NewServeMuxConfig(nil).Mux()
    63  
    64  	mux.Handle("/bar", safehttp.MethodGet, BarHandler)
    65  	mux.Handle("/more/bar", safehttp.MethodGet, safehttp.StripPrefix("/badprefix", BarHandler))
    66  
    67  	r := httptest.NewRequest(safehttp.MethodGet, "http://foo.com/more/bar", nil)
    68  	rw := httptest.NewRecorder()
    69  
    70  	defer func() {
    71  		if r := recover(); r != nil {
    72  			return
    73  		}
    74  		t.Errorf("expected panic")
    75  	}()
    76  	mux.ServeHTTP(rw, r)
    77  }