home/internal/static/delivery/http/static_http_test.go

40 lines
1.1 KiB
Go

package http_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"source.toby3d.me/toby3d/home/internal/domain"
delivery "source.toby3d.me/toby3d/home/internal/static/delivery/http"
repository "source.toby3d.me/toby3d/home/internal/static/repository/stub"
"source.toby3d.me/toby3d/home/internal/static/usecase"
"source.toby3d.me/toby3d/home/internal/testutil"
)
func TestHandler_ServeHTTP(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(http.MethodGet, "/robots.txt", nil)
testStatic := domain.NewStatic(strings.NewReader("User-agent: *\nAllow: /"), time.Now().UTC(), "robots.txt")
staticService := usecase.NewStaticUseCase(repository.NewStubStaticRepository(nil, testStatic, false))
handler, err := delivery.NewHandler(staticService).Handle(context.Background(), req.URL.Path)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp := w.Result()
if expect := http.StatusOK; resp.StatusCode != expect {
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, expect)
}
testutil.GoldenEqual(t, resp.Body)
}