52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
package server_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"clintonambulance.com/calculate_negative_points/internal/config"
|
|
"clintonambulance.com/calculate_negative_points/internal/server"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
func TestServer(t *testing.T) {
|
|
RegisterFailHandler(Fail)
|
|
RunSpecs(t, "Http Suite")
|
|
}
|
|
|
|
var _ = Describe("NewHttpServer", func() {
|
|
It("Creates a new HttpServer", func() {
|
|
var logOutput bytes.Buffer
|
|
version := config.Version{Release: "test-version"}
|
|
logger, _ := config.NewLogger(version, &logOutput, []string{"cmd", "-e", "testEnvironment"})
|
|
srv, _ := server.NewHttpServer(logger, version)
|
|
srv.Method(http.MethodHead, "/dummy-route", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/foobar", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
// Validate that the server responds with the web frontend for unknown routes
|
|
srv.ServeHTTP(rec, req)
|
|
|
|
Expect(rec.Code).To(Equal(http.StatusNotFound))
|
|
Expect(rec.Header().Get("Content-Type")).To(Equal("application/json; charset=UTF-8"))
|
|
Expect(rec.Body.String()).To(ContainSubstring(`{"message":"Not Found"}`))
|
|
|
|
// Validate that it writes to the log for all requests
|
|
var logMessage map[string]interface{}
|
|
err := json.Unmarshal(logOutput.Bytes(), &logMessage)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(logMessage["msg"]).To(Equal("HTTP request processed"))
|
|
Expect(logMessage["method"]).To(Equal("GET"))
|
|
Expect(logMessage["uri"]).To(Equal("/foobar"))
|
|
Expect(logMessage["status"]).To(Equal(404.0))
|
|
})
|
|
})
|