server auth
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tim/cairnquire/apps/server/internal/config"
|
||||
"github.com/tim/cairnquire/apps/server/internal/docs"
|
||||
)
|
||||
|
||||
@@ -63,3 +68,56 @@ func TestTrimEditSuffix(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsContentAssetPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
want bool
|
||||
}{
|
||||
{path: "logo.webp", want: true},
|
||||
{path: "guide/logo.png", want: true},
|
||||
{path: "guide/page", want: false},
|
||||
{path: "guide/page.md", want: false},
|
||||
{path: "guide/page/edit", want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.path, func(t *testing.T) {
|
||||
if got := isContentAssetPath(tt.path); got != tt.want {
|
||||
t.Fatalf("isContentAssetPath(%q) = %t, want %t", tt.path, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleContentAssetServesImageFromSourceDir(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
image := []byte{
|
||||
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
|
||||
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
|
||||
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
|
||||
0x89,
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "logo.png"), image, 0o644); err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
|
||||
server := &Server{
|
||||
config: config.Config{
|
||||
Content: config.ContentConfig{SourceDir: root},
|
||||
},
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodGet, "/docs/logo.png", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
server.handleContentAsset(recorder, request, "logo.png")
|
||||
|
||||
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 != "image/png" {
|
||||
t.Fatalf("content-type = %q, want image/png", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user