|
1 |
| -// Copyright (c) 2021, Maxime Soulé |
| 1 | +// Copyright (c) 2022, Maxime Soulé |
2 | 2 | // All rights reserved.
|
3 | 3 | //
|
4 | 4 | // This source code is licensed under the BSD-style license found in the
|
|
7 | 7 | package internal
|
8 | 8 |
|
9 | 9 | import (
|
10 |
| - "bytes" |
| 10 | + "encoding/json" |
11 | 11 | "net/http"
|
12 |
| - "net/http/httputil" |
13 |
| - "testing" |
14 |
| - "unicode/utf8" |
| 12 | + "net/http/httptest" |
| 13 | + "sync" |
15 | 14 | )
|
16 | 15 |
|
17 |
| -// canBackquote is the same as strconv.CanBackquote but works on |
18 |
| -// []byte and accepts '\n' and '\r'. |
19 |
| -func canBackquote(b []byte) bool { |
20 |
| - for len(b) > 0 { |
21 |
| - r, wid := utf8.DecodeRune(b) |
22 |
| - b = b[wid:] |
23 |
| - if wid > 1 { |
24 |
| - if r == '\ufeff' { |
25 |
| - return false // BOMs are invisible and should not be quoted. |
26 |
| - } |
27 |
| - continue // All other multibyte runes are correctly encoded and assumed printable. |
28 |
| - } |
29 |
| - if r == utf8.RuneError { |
30 |
| - return false |
31 |
| - } |
32 |
| - if (r < ' ' && r != '\t' && r != '\n' && r != '\r') || r == '`' || r == '\u007F' { |
33 |
| - return false |
34 |
| - } |
35 |
| - } |
36 |
| - return true |
37 |
| -} |
| 16 | +type Response struct { |
| 17 | + sync.Mutex |
38 | 18 |
|
39 |
| -func replaceCrLf(b []byte) []byte { |
40 |
| - return bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n")) |
| 19 | + name string |
| 20 | + response *httptest.ResponseRecorder |
| 21 | + |
| 22 | + asJSON any |
| 23 | + jsonDecoded bool |
41 | 24 | }
|
42 | 25 |
|
43 |
| -func backquote(b []byte) ([]byte, bool) { |
44 |
| - // if there is as many \r\n as \n, replace all occurrences by \n |
45 |
| - // so we can conveniently print the buffer inside `…`. |
46 |
| - crnl := bytes.Count(b, []byte("\r\n")) |
47 |
| - cr := bytes.Count(b, []byte("\r")) |
48 |
| - if crnl != 0 { |
49 |
| - nl := bytes.Count(b, []byte("\n")) |
50 |
| - if crnl != nl || crnl != cr { |
51 |
| - return nil, false |
52 |
| - } |
53 |
| - return replaceCrLf(b), true |
| 26 | +func NewResponse(resp *httptest.ResponseRecorder) *Response { |
| 27 | + return &Response{ |
| 28 | + response: resp, |
54 | 29 | }
|
55 |
| - |
56 |
| - return b, cr == 0 |
57 | 30 | }
|
58 | 31 |
|
59 |
| -// DumpResponse logs "resp" using Logf method of "t". |
60 |
| -// |
61 |
| -// It tries to produce a result as readable as possible first using |
62 |
| -// backquotes then falling back to double-quotes. |
63 |
| -func DumpResponse(t testing.TB, resp *http.Response) { |
64 |
| - t.Helper() |
| 32 | +func (r *Response) Response() *http.Response { |
| 33 | + // No lock needed here |
| 34 | + return r.response.Result() |
| 35 | +} |
65 | 36 |
|
66 |
| - const label = "Received response:\n" |
67 |
| - b, _ := httputil.DumpResponse(resp, true) |
68 |
| - if canBackquote(b) { |
69 |
| - bodyPos := bytes.Index(b, []byte("\r\n\r\n")) |
| 37 | +func (r *Response) UnmarshalJSON() (any, error) { |
| 38 | + r.Lock() |
| 39 | + defer r.Unlock() |
70 | 40 |
|
71 |
| - if body, ok := backquote(b[bodyPos+4:]); ok { |
72 |
| - headers := replaceCrLf(b[:bodyPos]) |
73 |
| - t.Logf(label+"`%s\n\n%s`", headers, body) |
74 |
| - return |
| 41 | + if !r.jsonDecoded { |
| 42 | + err := json.Unmarshal(r.response.Body.Bytes(), &r.asJSON) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
75 | 45 | }
|
| 46 | + r.jsonDecoded = true |
76 | 47 | }
|
77 | 48 |
|
78 |
| - t.Logf(label+"%q", b) |
| 49 | + return r.asJSON, nil |
79 | 50 | }
|
0 commit comments