|
1 | 1 | package room |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 | ) |
6 | 7 |
|
7 | 8 | func TestJsonBody_Parse(t *testing.T) { |
| 9 | + // Test case where JSON encoding is successful |
8 | 10 | data := map[string]interface{}{"key": "value"} |
9 | | - body := NewJsonBodyParser(data).Parse() |
10 | | - |
11 | | - if body == nil { |
12 | | - t.Error("Expected a non-nil buffer, but got nil") |
| 11 | + body := JsonBody{v: data} |
| 12 | + buffer := body.Parse() |
| 13 | + expected := `{"key":"value"}` |
| 14 | + bufferString := strings.TrimRight(buffer.String(), "\n") // Remove trailing newline |
| 15 | + if bufferString != expected { |
| 16 | + t.Errorf("JsonBody Parse() returned %s, expected %s", bufferString, expected) |
13 | 17 | } |
14 | 18 |
|
15 | | - // Optionally, you can check the content of the buffer or other expectations. |
| 19 | + // Test case where JSON encoding fails |
| 20 | + defer func() { |
| 21 | + if r := recover(); r == nil { |
| 22 | + t.Error("JsonBody Parse() did not panic when JSON encoding failed") |
| 23 | + } |
| 24 | + }() |
| 25 | + invalidData := make(chan int) // Invalid data for JSON encoding |
| 26 | + invalidBody := JsonBody{v: invalidData} |
| 27 | + _ = invalidBody.Parse() |
16 | 28 | } |
17 | 29 |
|
18 | | -type testStruct struct { |
19 | | - Key string `url:"key"` |
| 30 | +func TestFormURLEncodedBodyAsStruct_Parse(t *testing.T) { |
| 31 | + mapData := struct { |
| 32 | + Key1 string `url:"key1"` |
| 33 | + Key2 int `url:"key2"` |
| 34 | + }{"value1", 42} |
| 35 | + body := FormURLEncodedBody{v: mapData} |
| 36 | + buffer := body.Parse() |
| 37 | + expected := "key1=value1&key2=42" |
| 38 | + if buffer.String() != expected { |
| 39 | + t.Errorf("FormURLEncodedBody Parse() returned %s, expected %s", buffer.String(), expected) |
| 40 | + } |
20 | 41 | } |
21 | 42 |
|
22 | | -func TestFormURLEncodedBody_Parse(t *testing.T) { |
23 | | - data := testStruct{"value"} |
24 | | - body := NewFormURLEncodedBodyParser(data).Parse() |
25 | | - |
26 | | - if body == nil { |
27 | | - t.Error("Expected a non-nil buffer, but got nil") |
| 43 | +func TestFormURLEncodedBodyAsMap_Parse(t *testing.T) { |
| 44 | + mapData := map[string]any{"key1": "value1", "key2": "42"} |
| 45 | + body := FormURLEncodedBody{v: mapData} |
| 46 | + buffer := body.Parse() |
| 47 | + expected := "key1=value1&key2=42" |
| 48 | + if buffer.String() != expected { |
| 49 | + t.Errorf("FormURLEncodedBody Parse() returned %s, expected %s", buffer.String(), expected) |
28 | 50 | } |
| 51 | +} |
29 | 52 |
|
30 | | - // Optionally, you can check the content of the buffer or other expectations. |
| 53 | +func TestDumpBody_Parse(t *testing.T) { |
| 54 | + // Test DumpBody Parse() always returns an empty buffer |
| 55 | + body := dumpBody{} |
| 56 | + buffer := body.Parse() |
| 57 | + expected := "" |
| 58 | + if buffer.String() != expected { |
| 59 | + t.Errorf("DumpBody Parse() returned %s, expected empty", buffer.String()) |
| 60 | + } |
31 | 61 | } |
32 | 62 |
|
33 | | -func TestFormURLEncodedBody_Parse_Error(t *testing.T) { |
34 | | - // Test case where query.Values returns an error |
35 | | - data := map[string]interface{}{"key": make(chan int)} // invalid type to trigger an error |
36 | | - defer func() { |
37 | | - if r := recover(); r == nil { |
38 | | - t.Error("Expected a panic, but no panic occurred") |
39 | | - } |
40 | | - }() |
| 63 | +func TestNewJsonBodyParser(t *testing.T) { |
| 64 | + // Test NewJsonBodyParser() creates a JsonBody instance |
| 65 | + data := map[string]interface{}{"key": "value"} |
| 66 | + bodyParser := NewJsonBodyParser(data) |
| 67 | + _, ok := bodyParser.(JsonBody) |
| 68 | + if !ok { |
| 69 | + t.Error("NewJsonBodyParser() did not return a JsonBody instance") |
| 70 | + } |
| 71 | +} |
41 | 72 |
|
42 | | - _ = NewFormURLEncodedBodyParser(data).Parse() |
| 73 | +func TestNewFormURLEncodedBodyParser(t *testing.T) { |
| 74 | + // Test NewFormURLEncodedBodyParser() creates a FormURLEncodedBody instance |
| 75 | + data := map[string]interface{}{"key": "value"} |
| 76 | + bodyParser := NewFormURLEncodedBodyParser(data) |
| 77 | + _, ok := bodyParser.(FormURLEncodedBody) |
| 78 | + if !ok { |
| 79 | + t.Error("NewFormURLEncodedBodyParser() did not return a FormURLEncodedBody instance") |
| 80 | + } |
43 | 81 | } |
0 commit comments