MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestStatic

Function TestStatic

pkg/web/web_test.go:43–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

41}
42
43func TestStatic(t *testing.T) {
44 a := assertions.New(t)
45 s, err := New(test.Context())
46 if !a.So(err, should.BeNil) {
47 t.Fatal("Could not create a web instance")
48 }
49
50 dir, err := os.Getwd()
51
52 if !a.So(err, should.BeNil) {
53 t.Fatal("Could not create resolve testing directory")
54 }
55
56 s.Static("/assets", http.Dir(dir))
57
58 // HTTP server returns 200 on valid file request
59 {
60 req := httptest.NewRequest(http.MethodGet, "/assets/web_test.go", nil)
61 rec := httptest.NewRecorder()
62
63 s.ServeHTTP(rec, req)
64
65 resp := rec.Result()
66 body, _ := io.ReadAll(resp.Body)
67
68 a.So(resp.StatusCode, should.Equal, http.StatusOK)
69 a.So(strings.HasPrefix(string(body), "//"), should.BeTrue)
70 }
71
72 // HTTP server returns 404 on invalid file request
73 {
74 req := httptest.NewRequest(http.MethodGet, "/assets/null.txt", nil)
75 rec := httptest.NewRecorder()
76
77 s.Static("/assets", http.Dir(dir+"/teststatic"))
78
79 s.ServeHTTP(rec, req)
80
81 resp := rec.Result()
82 a.So(resp.StatusCode, should.Equal, http.StatusNotFound)
83 }
84}
85
86func TestCookies(t *testing.T) {
87 a := assertions.New(t)

Callers

nothing calls this directly

Calls 9

ContextFunction · 0.92
StaticMethod · 0.80
DirMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
FatalMethod · 0.65
ResultMethod · 0.65
ServeHTTPMethod · 0.45
HasPrefixMethod · 0.45

Tested by

no test coverage detected