|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// TestRun_ServerIntegration starts the real HTTP server, exercises endpoints, then shuts it down. |
| 14 | +func TestRun_ServerIntegration(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + // Pick a free port |
| 18 | + lc := net.ListenConfig{} |
| 19 | + ln, err := lc.Listen(t.Context(), "tcp", ":0") |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("listen :0: %v", err) |
| 22 | + } |
| 23 | + port := ln.Addr().(*net.TCPAddr).Port |
| 24 | + if err := ln.Close(); err != nil { |
| 25 | + t.Fatalf("close listener: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + configContent := `routes: |
| 29 | + - pattern: /hello |
| 30 | + responses: |
| 31 | + - code: 200 |
| 32 | + body: Hello |
| 33 | + - pattern: /json |
| 34 | + responses: |
| 35 | + - code: 201 |
| 36 | + body: '{"ok":true}' |
| 37 | + is_json: true |
| 38 | +` |
| 39 | + cfgPath := writeConfig(t, configContent) |
| 40 | + ctx, cancel := context.WithCancel(t.Context()) |
| 41 | + |
| 42 | + done := make(chan struct{}) |
| 43 | + go func() { |
| 44 | + defer close(done) |
| 45 | + err := run(ctx, []string{"-c", cfgPath, "-p", strconv.Itoa(port)}) |
| 46 | + if err != nil { |
| 47 | + t.Errorf("run: %v", err) |
| 48 | + } |
| 49 | + }() |
| 50 | + |
| 51 | + client := &http.Client{Timeout: 2 * time.Second} |
| 52 | + base := fmt.Sprintf("http://localhost:%d", port) |
| 53 | + |
| 54 | + deadline := time.Now().Add(5 * time.Second) |
| 55 | + for { |
| 56 | + if time.Now().After(deadline) { |
| 57 | + t.Fatalf("server did not start in time on %s", base) |
| 58 | + } |
| 59 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, base+"/hello", http.NoBody) |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("new request: %v", err) |
| 62 | + } |
| 63 | + resp, err := client.Do(req) |
| 64 | + if err != nil { |
| 65 | + time.Sleep(50 * time.Millisecond) |
| 66 | + continue |
| 67 | + } |
| 68 | + _ = resp.Body.Close() |
| 69 | + break |
| 70 | + } |
| 71 | + |
| 72 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, base+"/hello", http.NoBody) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("new request: %v", err) |
| 75 | + } |
| 76 | + resp, err := client.Do(req) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("GET /hello: %v", err) |
| 79 | + } |
| 80 | + if resp.StatusCode != http.StatusOK { |
| 81 | + t.Fatalf("/hello expected 200 got %d", resp.StatusCode) |
| 82 | + } |
| 83 | + _ = resp.Body.Close() |
| 84 | + |
| 85 | + req, err = http.NewRequestWithContext(t.Context(), http.MethodGet, base+"/json", http.NoBody) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("new request: %v", err) |
| 88 | + } |
| 89 | + resp, err = client.Do(req) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("GET /json: %v", err) |
| 92 | + } |
| 93 | + if resp.StatusCode != http.StatusCreated { |
| 94 | + t.Fatalf("/json expected 201 got %d", resp.StatusCode) |
| 95 | + } |
| 96 | + if ct := resp.Header.Get("Content-Type"); ct != JsonContentType { |
| 97 | + t.Fatalf("expected application/json got %q", ct) |
| 98 | + } |
| 99 | + _ = resp.Body.Close() |
| 100 | + |
| 101 | + req, err = http.NewRequestWithContext(t.Context(), http.MethodGet, base+"/", http.NoBody) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("new request: %v", err) |
| 104 | + } |
| 105 | + resp, err = client.Do(req) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("GET /: %v", err) |
| 108 | + } |
| 109 | + if resp.StatusCode != http.StatusNotFound { |
| 110 | + t.Fatalf("/ expected 404 got %d", resp.StatusCode) |
| 111 | + } |
| 112 | + _ = resp.Body.Close() |
| 113 | + |
| 114 | + cancel() |
| 115 | + |
| 116 | + select { |
| 117 | + case <-done: |
| 118 | + case <-time.After(5 * time.Second): |
| 119 | + t.Fatalf("server did not exit in time") |
| 120 | + } |
| 121 | +} |
0 commit comments