acc: Print full URL and request body for missing handlers (#2394)

## Tests
Manually, I have a test that fails.

Before:

```
=== NAME  TestAccept
    server.go:195:

        ----------------------------------------
        No stub found for pattern: GET /api/2.1/clusters/get

        To stub a response for this request, you can add
        the following to test.toml:
        [[Server]]
        Pattern = "GET /api/2.1/clusters/get"
        Response.Body = '''
        <response body here>
        '''
        Response.StatusCode = <response status-code here>
        ----------------------------------------
```

After:
```
    server.go:203: No handler for URL: /api/2.1/clusters/get?cluster_id=0717-132531-5opeqon1
        Body: [0 bytes]

        For acceptance tests, add this to test.toml:
        [[Server]]
        Pattern = "GET /api/2.1/clusters/get"
        Response.Body = '<response body here>'
        # Response.StatusCode = <response code if not 200>
```
This commit is contained in:
Denis Bilenko 2025-02-27 14:07:48 +01:00 committed by GitHub
parent e2db0cd0e2
commit c1f835f951
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package testserver
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -191,24 +192,23 @@ func New(t testutil.TestingT) *Server {
// Set up the not found handler as fallback // Set up the not found handler as fallback
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pattern := r.Method + " " + r.URL.Path pattern := r.Method + " " + r.URL.Path
bodyBytes, err := io.ReadAll(r.Body)
var body string
if err != nil {
body = fmt.Sprintf("failed to read the body: %s", err)
} else {
body = fmt.Sprintf("[%d bytes] %s", len(bodyBytes), bodyBytes)
}
t.Errorf(` t.Errorf(`No handler for URL: %s
Body: %s
---------------------------------------- For acceptance tests, add this to test.toml:
No stub found for pattern: %s
To stub a response for this request, you can add
the following to test.toml:
[[Server]] [[Server]]
Pattern = %q Pattern = %q
Response.Body = ''' Response.Body = '<response body here>'
<response body here> # Response.StatusCode = <response code if not 200>
''' `, r.URL, body, pattern)
Response.StatusCode = <response status-code here>
----------------------------------------
`, pattern, pattern)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotImplemented) w.WriteHeader(http.StatusNotImplemented)