-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
48 lines (39 loc) · 1009 Bytes
/
server_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
43
44
45
46
47
48
//go:build !js
package httplog
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/sirupsen/logrus"
)
// TestHTTPLogServer tests logging http requests to logrus.
func TestHTTPLogServer(t *testing.T) {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
handler := http.NewServeMux()
handler.HandleFunc("/test", func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(200)
_, _ = rw.Write([]byte("hello world!\n"))
})
srv := httptest.NewServer(LoggingMiddleware(handler, le, LoggingMiddlewareOpts{UserAgent: true}))
defer srv.Close()
baseURL, _ := url.Parse(srv.URL)
baseURL = baseURL.JoinPath("test")
// Create the client
client := srv.Client()
// Get the test endpoint
resp, err := client.Get(baseURL.String())
if err != nil {
t.Fatal(err.Error())
}
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err.Error())
}
if string(data) != "hello world!\n" || resp.StatusCode != 200 {
t.Fail()
}
}