-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmisc_test.go
42 lines (38 loc) · 1 KB
/
misc_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
package httptools
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestDiscardPathElements_TrailingSlash(t *testing.T) {
ms := List{
DiscardPathElements(2),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Path", r.URL.Path)
}),
}
rr := httptest.NewRecorder()
ms.ServeHTTP(rr, MustRequest(http.NewRequest("GET", "/some/thing/different/yet/", nil)))
expected := []string{"/different/yet/"}
got := rr.HeaderMap["X-Path"]
if !reflect.DeepEqual(got, expected) {
t.Fatalf("Header list wrong. Expected %#v, got %#v", expected, got)
}
}
func ExampleDiscardPathElements() {
ms := List{
DiscardPathElements(2),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
}),
}
req, _ := http.NewRequest("GET", "/prefix/and/a/real/path", nil)
ms.ServeHTTP(httptest.NewRecorder(), req)
req, _ = http.NewRequest("GET", "/", nil)
ms.ServeHTTP(httptest.NewRecorder(), req)
// Output:
// /a/real/path
// /
}