|
| 1 | +package httpclient_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + "the-dev-tools/server/pkg/httpclient" |
| 9 | + "the-dev-tools/server/pkg/model/mexamplerespheader" |
| 10 | +) |
| 11 | + |
| 12 | +func TestConvertResponseToVar(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + input httpclient.Response |
| 16 | + expected httpclient.ResponseVar |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "Valid JSON body", |
| 20 | + input: httpclient.Response{ |
| 21 | + StatusCode: http.StatusOK, |
| 22 | + Body: []byte(`{"key": "value", "number": 123}`), |
| 23 | + Headers: []mexamplerespheader.ExampleRespHeader{ |
| 24 | + {HeaderKey: "Content-Type", Value: "application/json"}, |
| 25 | + {HeaderKey: "X-Request-Id", Value: "abc-123"}, |
| 26 | + }, |
| 27 | + }, |
| 28 | + expected: httpclient.ResponseVar{ |
| 29 | + StatusCode: http.StatusOK, |
| 30 | + Body: map[string]any{ |
| 31 | + "key": "value", |
| 32 | + "number": json.Number("123"), // Use json.Number for comparison |
| 33 | + }, |
| 34 | + Headers: map[string]string{ |
| 35 | + "Content-Type": "application/json", |
| 36 | + "X-Request-Id": "abc-123", |
| 37 | + }, |
| 38 | + Duration: 0, // Duration is not set by this function |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Non-JSON body", |
| 43 | + input: httpclient.Response{ |
| 44 | + StatusCode: http.StatusNotFound, |
| 45 | + Body: []byte("This is plain text"), |
| 46 | + Headers: []mexamplerespheader.ExampleRespHeader{ |
| 47 | + {HeaderKey: "Content-Type", Value: "text/plain"}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + expected: httpclient.ResponseVar{ |
| 51 | + StatusCode: http.StatusNotFound, |
| 52 | + Body: "This is plain text", |
| 53 | + Headers: map[string]string{ |
| 54 | + "Content-Type": "text/plain", |
| 55 | + }, |
| 56 | + Duration: 0, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Empty body and no headers", |
| 61 | + input: httpclient.Response{ |
| 62 | + StatusCode: http.StatusNoContent, |
| 63 | + Body: []byte{}, |
| 64 | + Headers: []mexamplerespheader.ExampleRespHeader{}, |
| 65 | + }, |
| 66 | + expected: httpclient.ResponseVar{ |
| 67 | + StatusCode: http.StatusNoContent, |
| 68 | + Body: "", |
| 69 | + Headers: map[string]string{}, |
| 70 | + Duration: 0, |
| 71 | + }, |
| 72 | + }, |
| 73 | + // Add more test cases as needed, e.g., malformed JSON |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + actual := httpclient.ConvertResponseToVar(tt.input) |
| 79 | + |
| 80 | + // Special handling for JSON body comparison due to potential type differences (e.g., float64 vs json.Number) |
| 81 | + if expectedBodyMap, ok := tt.expected.Body.(map[string]any); ok { |
| 82 | + if actualBodyMap, ok := actual.Body.(map[string]any); ok { |
| 83 | + // Marshal both to JSON strings for a robust comparison |
| 84 | + expectedJSON, _ := json.Marshal(expectedBodyMap) |
| 85 | + actualJSON, _ := json.Marshal(actualBodyMap) |
| 86 | + if string(expectedJSON) != string(actualJSON) { |
| 87 | + t.Errorf("ConvertResponseToVar() Body = %v, want %v", string(actualJSON), string(expectedJSON)) |
| 88 | + } |
| 89 | + // Avoid comparing Body again in DeepEqual |
| 90 | + tt.expected.Body = nil |
| 91 | + actual.Body = nil |
| 92 | + } else { |
| 93 | + t.Errorf("ConvertResponseToVar() Body type mismatch: expected map[string]any, got %T", actual.Body) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if !reflect.DeepEqual(actual, tt.expected) { |
| 98 | + t.Errorf("ConvertResponseToVar() = %v, want %v", actual, tt.expected) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
0 commit comments