45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/auth"
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/config"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestSDKFilesAreServedOverHTTP(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
frontend := fstest.MapFS{
|
|
"index.html": &fstest.MapFile{Data: []byte("<html>ok</html>")},
|
|
}
|
|
sdkFS := fstest.MapFS{
|
|
"index.js": &fstest.MapFile{Data: []byte("export function create() {}")},
|
|
"index.iife.js": &fstest.MapFile{Data: []byte("var IqudooSalesScenario = {};")},
|
|
}
|
|
handler := New(nil, auth.NewService(nil, config.AuthConfig{}), frontend, sdkFS, zap.NewNop(), "test")
|
|
|
|
for _, path := range []string{"/sdk/index.js", "/sdk/index.iife.js"} {
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
rec := httptest.NewRecorder()
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET %s status = %d, want %d", path, rec.Code, http.StatusOK)
|
|
}
|
|
if rec.Header().Get("Access-Control-Allow-Origin") != "*" {
|
|
t.Fatalf("GET %s missing open CORS header", path)
|
|
}
|
|
if got := rec.Header().Get("Content-Type"); got != "text/javascript; charset=utf-8" {
|
|
t.Fatalf("GET %s Content-Type = %q, want text/javascript", path, got)
|
|
}
|
|
if rec.Body.Len() == 0 {
|
|
t.Fatalf("GET %s returned empty body", path)
|
|
}
|
|
}
|
|
}
|