commit d6f9ef376e9c1665a81707549b9e9acb9cf75d06
parent 9257d091f6435ea480c9994887a5525db8850ef2
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date: Sat, 19 Aug 2023 21:59:26 +0200
test(server): test curlable & regular UI
Diffstat:
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/server.go b/server.go
@@ -15,7 +15,7 @@ type Server struct {
}
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
- userAgent := req.Header.Get("user-agent");
+ userAgent := req.Header.Get("user-agent")
if strings.Contains(userAgent, "curl") {
w.Header().Set("Content-Type", "text/plain")
diff --git a/server_test.go b/server_test.go
@@ -18,15 +18,21 @@ func TestServer(t *testing.T) {
server := &Server{conf}
+ // ResponseRecorder is an implementation of http.ResponseWriter that
+ // records its mutations for later inspection in tests.
+ // https://pkg.go.dev/net/http/httptest
rr := httptest.NewRecorder()
request, err := http.NewRequest("GET", "/", nil)
+
+ // test curlable UI
+ request.Header.Set("user-agent", "curl/8.1.2")
server.ServeHTTP(rr, request)
if err != nil {
t.Fatal("error creating new request:", err)
}
-
+
if status := rr.Code; status != http.StatusOK {
t.Errorf("request to / returned wrong response code. Got %v, want %v",
status, http.StatusOK)
@@ -36,4 +42,22 @@ func TestServer(t *testing.T) {
if !strings.Contains(welcome, "hive") {
t.Errorf("request to / does not contain word 'hive'")
}
+
+ // test regular browser UI
+ request.Header.Set("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
+ server.ServeHTTP(rr, request)
+
+ if err != nil {
+ t.Fatal("error creating new request:", err)
+ }
+
+ if status := rr.Code; status != http.StatusOK {
+ t.Errorf("request to / returned wrong response code. Got %v, want %v",
+ status, http.StatusOK)
+ }
+
+ welcome = rr.Body.String()
+ if !strings.Contains(welcome, "hive") {
+ t.Errorf("request to / does not contain word 'hive'")
+ }
}