generated from UCLALibrary/service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
39 lines (29 loc) · 971 Bytes
/
main_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
//go:build unit
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHelloWorld(t *testing.T) {
app := NewApp()
app.Routes()
// Create a response recorder to record the response
rec := httptest.NewRecorder()
// Create a GET test request
req, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err, "Failed to create request")
// Call the handler directly
app.Router.ServeHTTP(rec, req)
// Check the response status code and body
assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200")
assert.Equal(t, "hello world", rec.Body.String(), "Unexpected response body")
// Create a POST test request
req, err = http.NewRequest("POST", "/", nil)
// A new recorder must be created
rec = httptest.NewRecorder()
assert.NoError(t, err, "Failed to create request")
app.Router.ServeHTTP(rec, req)
assert.Equal(t, http.StatusNotFound, rec.Code, "Expected status code 404")
}