package httpserver

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestHandleServiceWorkerServesJavaScript(t *testing.T) {
	server := &Server{}
	req := httptest.NewRequest(http.MethodGet, "/sw.js", nil)
	recorder := httptest.NewRecorder()

	server.handleServiceWorker(recorder, req)

	response := recorder.Result()
	if response.StatusCode != http.StatusOK {
		t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
	}
	if got := response.Header.Get("Content-Type"); got != "application/javascript; charset=utf-8" {
		t.Fatalf("Content-Type = %q, want application/javascript; charset=utf-8", got)
	}
	if got := response.Header.Get("Service-Worker-Allowed"); got != "/" {
		t.Fatalf("Service-Worker-Allowed = %q, want /", got)
	}
	if recorder.Body.Len() == 0 {
		t.Fatal("expected non-empty service worker body")
	}
}
