| 41 | } |
| 42 | |
| 43 | func 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 | |
| 86 | func TestCookies(t *testing.T) { |
| 87 | a := assertions.New(t) |