|
| 1 | +// Copyright (c) 2025-2026 HP Development Company, L.P. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package petstoretest |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "encoding/json" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +// Sanity tests for the fake server. These run every time (no gate) because |
| 16 | +// they are self-contained and fast no external tcli invocation involved. |
| 17 | + |
| 18 | +func TestServer_AddThenGet(t *testing.T) { |
| 19 | + srv := NewServer() |
| 20 | + defer srv.Close() |
| 21 | + |
| 22 | + got := doJSON(t, srv.URL, "POST", "/v2/pet", `{"id":7,"name":"a","photoUrls":["x"]}`) |
| 23 | + if got["id"] != float64(7) { |
| 24 | + t.Errorf("addPet returned id=%v, want 7", got["id"]) |
| 25 | + } |
| 26 | + got = doJSON(t, srv.URL, "GET", "/v2/pet/7", "") |
| 27 | + if got["name"] != "a" { |
| 28 | + t.Errorf("getPetById returned name=%v, want a", got["name"]) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestServer_GetMissingReturns404(t *testing.T) { |
| 33 | + srv := NewServer() |
| 34 | + defer srv.Close() |
| 35 | + resp := do(t, srv.URL, "GET", "/v2/pet/999", "") |
| 36 | + defer resp.Body.Close() |
| 37 | + if resp.StatusCode != http.StatusNotFound { |
| 38 | + t.Errorf("status = %d, want 404", resp.StatusCode) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestServer_DeleteThenGet404(t *testing.T) { |
| 43 | + srv := NewServer() |
| 44 | + defer srv.Close() |
| 45 | + doJSON(t, srv.URL, "POST", "/v2/pet", `{"id":1,"name":"x","photoUrls":[]}`) |
| 46 | + got := doJSON(t, srv.URL, "DELETE", "/v2/pet/1", "") |
| 47 | + if got["message"] != "1" { |
| 48 | + t.Errorf("deletePet.message = %v, want \"1\" (id echoed back)", got["message"]) |
| 49 | + } |
| 50 | + resp := do(t, srv.URL, "GET", "/v2/pet/1", "") |
| 51 | + defer resp.Body.Close() |
| 52 | + if resp.StatusCode != http.StatusNotFound { |
| 53 | + t.Errorf("after delete, GET status = %d, want 404", resp.StatusCode) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestServer_FindByStatusReturnsArray(t *testing.T) { |
| 58 | + srv := NewServer() |
| 59 | + defer srv.Close() |
| 60 | + doJSON(t, srv.URL, "POST", "/v2/pet", `{"id":1,"name":"a","photoUrls":[],"status":"available"}`) |
| 61 | + doJSON(t, srv.URL, "POST", "/v2/pet", `{"id":2,"name":"b","photoUrls":[],"status":"sold"}`) |
| 62 | + |
| 63 | + resp := do(t, srv.URL, "GET", "/v2/pet/findByStatus?status=available", "") |
| 64 | + defer resp.Body.Close() |
| 65 | + var arr []map[string]any |
| 66 | + if err := json.NewDecoder(resp.Body).Decode(&arr); err != nil { |
| 67 | + t.Fatalf("decode array: %v", err) |
| 68 | + } |
| 69 | + if len(arr) != 1 || arr[0]["id"] != float64(1) { |
| 70 | + t.Errorf("findByStatus(available) = %+v, want single pet id=1", arr) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func do(t *testing.T, base, method, path, body string) *http.Response { |
| 75 | + t.Helper() |
| 76 | + var b io.Reader |
| 77 | + if body != "" { |
| 78 | + b = strings.NewReader(body) |
| 79 | + } |
| 80 | + req, err := http.NewRequest(method, base+path, b) |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + if body != "" { |
| 85 | + req.Header.Set("Content-Type", "application/json") |
| 86 | + } |
| 87 | + resp, err := http.DefaultClient.Do(req) |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + return resp |
| 92 | +} |
| 93 | + |
| 94 | +func doJSON(t *testing.T, base, method, path, body string) map[string]any { |
| 95 | + t.Helper() |
| 96 | + resp := do(t, base, method, path, body) |
| 97 | + defer resp.Body.Close() |
| 98 | + var buf bytes.Buffer |
| 99 | + _, _ = io.Copy(&buf, resp.Body) |
| 100 | + if resp.StatusCode >= 300 { |
| 101 | + t.Fatalf("%s %s -> %d: %s", method, path, resp.StatusCode, buf.String()) |
| 102 | + } |
| 103 | + var out map[string]any |
| 104 | + if err := json.Unmarshal(buf.Bytes(), &out); err != nil { |
| 105 | + t.Fatalf("decode %s %s: %v (body=%q)", method, path, err, buf.String()) |
| 106 | + } |
| 107 | + return out |
| 108 | +} |
0 commit comments