85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
package openapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestSpecificationCoversRegisteredV1Operations(t *testing.T) {
|
|
var document struct {
|
|
OpenAPI string `json:"openapi"`
|
|
Security []map[string][]string `json:"security"`
|
|
Paths map[string]map[string]any `json:"paths"`
|
|
Components map[string]json.RawMessage `json:"components"`
|
|
}
|
|
if err := json.Unmarshal(Spec(), &document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if document.OpenAPI != "3.1.0" {
|
|
t.Fatalf("openapi version = %q", document.OpenAPI)
|
|
}
|
|
if len(document.Security) != 1 {
|
|
t.Fatalf("security = %#v", document.Security)
|
|
}
|
|
expected := map[string]string{
|
|
"/openapi/v1/openapi.json": "get",
|
|
"/openapi/v1/generations/text": "post",
|
|
"/openapi/v1/generations/image": "post",
|
|
"/openapi/v1/generations": "get",
|
|
"/openapi/v1/generations/{id}": "get",
|
|
"/openapi/v1/generations/{id}/inputs/{inputID}": "get",
|
|
"/openapi/v1/generations/{id}/outputs/{outputID}": "get",
|
|
"/openapi/v1/generations/{id}/outputs/{outputID}/thumbnail": "get",
|
|
}
|
|
if len(document.Paths) != len(expected) {
|
|
t.Fatalf("path count = %d, want %d", len(document.Paths), len(expected))
|
|
}
|
|
for path, method := range expected {
|
|
operation, exists := document.Paths[path][method]
|
|
if !exists {
|
|
t.Errorf("missing %s %s", method, path)
|
|
continue
|
|
}
|
|
encoded, err := json.Marshal(operation)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var shape struct {
|
|
Responses map[string]json.RawMessage `json:"responses"`
|
|
}
|
|
if err := json.Unmarshal(encoded, &shape); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, exists := shape.Responses["429"]; !exists {
|
|
t.Errorf("missing 429 response for %s %s", method, path)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSpecReturnsDefensiveCopy(t *testing.T) {
|
|
first := Spec()
|
|
first[0] = 'x'
|
|
if second := Spec(); len(second) == 0 || second[0] != '{' {
|
|
t.Fatal("Spec returned mutable embedded storage")
|
|
}
|
|
}
|
|
|
|
func TestGenerationSchemaIncludesNullableStartedAt(t *testing.T) {
|
|
var document map[string]any
|
|
if err := json.Unmarshal(Spec(), &document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
components := document["components"].(map[string]any)
|
|
schemas := components["schemas"].(map[string]any)
|
|
generation := schemas["Generation"].(map[string]any)
|
|
properties := generation["properties"].(map[string]any)
|
|
startedAt, ok := properties["started_at"].(map[string]any)
|
|
if !ok || startedAt["format"] != "date-time" {
|
|
t.Fatalf("started_at schema = %#v", properties["started_at"])
|
|
}
|
|
types, ok := startedAt["type"].([]any)
|
|
if !ok || len(types) != 2 || types[0] != "string" || types[1] != "null" {
|
|
t.Fatalf("started_at type = %#v", startedAt["type"])
|
|
}
|
|
}
|