85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package viewer
|
|
|
|
import (
|
|
"bytes"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRenderAcceptsMultipartSnapshotFile(t *testing.T) {
|
|
handler := NewStandaloneHandler(HandlerOptions{Title: "Reanimator Chart"})
|
|
|
|
var body bytes.Buffer
|
|
writer := multipart.NewWriter(&body)
|
|
part, err := writer.CreateFormFile("snapshot_file", "snapshot.json")
|
|
if err != nil {
|
|
t.Fatalf("CreateFormFile() error = %v", err)
|
|
}
|
|
|
|
snapshot := `{
|
|
"target_host": "upload-host",
|
|
"hardware": {
|
|
"board": {
|
|
"serial_number": "BOARD-002"
|
|
}
|
|
}
|
|
}`
|
|
if _, err := part.Write([]byte(snapshot)); err != nil {
|
|
t.Fatalf("Write() error = %v", err)
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
t.Fatalf("Close() error = %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/render", &body)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
rec := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
|
|
response := rec.Body.String()
|
|
for _, needle := range []string{"upload-host", "BOARD-002", "Board"} {
|
|
if !strings.Contains(response, needle) {
|
|
t.Fatalf("expected response to contain %q", needle)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEmbeddedHandlerRootHasNoUploadForm(t *testing.T) {
|
|
handler := NewHandler(HandlerOptions{Title: "Reanimator Chart"})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
if strings.Contains(rec.Body.String(), "multipart/form-data") {
|
|
t.Fatalf("expected embedded handler root to omit upload form")
|
|
}
|
|
}
|
|
|
|
func TestStandaloneHandlerRootShowsUploadForm(t *testing.T) {
|
|
handler := NewStandaloneHandler(HandlerOptions{Title: "Reanimator Chart"})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
body := rec.Body.String()
|
|
if !strings.Contains(body, "multipart/form-data") || !strings.Contains(body, "snapshot_file") {
|
|
t.Fatalf("expected standalone handler root to include upload form")
|
|
}
|
|
}
|