MCPcopy Create free account
hub / github.com/PuerkitoBio/fetchbot / TestMux

Function TestMux

handler_test.go:36–136  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

34}
35
36func TestMux(t *testing.T) {
37 // Start the servers
38 srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39 switch r.URL.Path {
40 case "/robots.txt":
41 w.Write([]byte(`
42User-agent: *
43Disallow: /deny
44`))
45 return
46 case "/204":
47 w.Header().Set("Content-Type", "text/plain")
48 w.WriteHeader(204)
49 w.Write(nil)
50 return
51 case "/4xx":
52 w.Header().Set("Content-Type", "text/plain")
53 w.WriteHeader(404)
54 w.Write(nil)
55 case "/json":
56 w.Header().Set("Content-Type", "application/json")
57 default:
58 w.Header().Set("Content-Type", "text/html; charset=utf-8")
59 }
60 w.Write([]byte("1"))
61 }))
62 defer srv1.Close()
63 srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
64 w.Write([]byte("ok"))
65 }))
66 defer srv2.Close()
67 srvu, err := url.Parse(srv1.URL)
68 if err != nil {
69 t.Fatal(err)
70 }
71 srv1Host := srvu.Host
72 srvu, err = url.Parse(srv2.URL)
73 if err != nil {
74 t.Fatal(err)
75 }
76 srv2Host := srvu.Host
77
78 // List of test cases
79 cases := []struct {
80 url string
81 trace string
82 }{
83 0: {srv2.URL + "/none", "d"}, // no specific handler, use default
84 1: {srv1.URL + "/json", "j"}, // json-specific handler
85 2: {srv1.URL + "/deny", "a"}, // ErrDisallowed, use any errors handler
86 3: {srv1.URL + "/a", "g"}, // GET text/html
87 4: {srv1.URL + "/204", "s"}, // status 204
88 5: {srv1.URL + "/4xx", "r"}, // status range 4xx
89 6: {srv1.URL + "/b", "p"}, // path-specific handler
90 7: {srv1.URL + "/baba", "q"}, // path-specific handler
91 8: {srv1.URL + "/b/c", "p"}, // path-specific handler
92 9: {srv2.URL + "/zz", "r"}, // custom predicate
93 }

Callers

nothing calls this directly

Calls 15

HandleErrorMethod · 0.95
HandleErrorsMethod · 0.95
ResponseMethod · 0.95
NewMuxFunction · 0.85
setTraceFunction · 0.85
NewFunction · 0.85
CloseMethod · 0.80
HandlerMethod · 0.80
ContentTypeMethod · 0.80
HostMethod · 0.80
StatusMethod · 0.80
StatusRangeMethod · 0.80

Tested by

no test coverage detected