package ops
import (
"context"
"io"
"strings"
"testing"
)
type specFile struct{}
func (specFile) Name() string { return "x.zip" }
func (specFile) ContentType() string { return "application/zip" }
func (specFile) ETag() string { return "" }
func (specFile) Immutable() bool { return false }
func (specFile) Open() (io.ReadCloser, int64, error) {
return io.NopCloser(strings.NewReader("")), 0, nil
}
type specFileIn struct {
Repo string `json:"repo"`
}
// An op whose answer is a File is documented as a binary download, not
// as a JSON object with the File's (unexported, empty) fields.
func TestAFileAnswerIsDocumentedAsBinary(t *testing.T) {
r := NewRegistry()
if err := Register(r, Op[specFileIn, specFile]{Name: "spec.file", Kind: Query, Auth: Anonymous,
Route: "GET /api/v1/specfile", Doc: "test double",
Run: func(context.Context, specFileIn) (specFile, error) { return specFile{}, nil }}); err != nil {
t.Fatal(err)
}
doc := specFor(t, r)
resp := doc["paths"].(map[string]any)["/api/v1/specfile"].(map[string]any)["get"].(map[string]any)["responses"].(map[string]any)["200"].(map[string]any)
content := resp["content"].(map[string]any)
if _, ok := content["application/octet-stream"]; !ok {
t.Fatalf("FAIL: a File answer is documented as %v", content)
}
if _, ok := content["application/json"]; ok {
t.Fatal("FAIL: a File answer is also documented as JSON")
}
t.Logf("ok: %v", resp["description"])
}