|
17 | 17 | package util_test
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "bytes" |
20 | 21 | "encoding/json"
|
21 | 22 | "errors"
|
22 | 23 | "html/template"
|
23 | 24 | "io"
|
| 25 | + "mime/multipart" |
24 | 26 | "net/http"
|
25 | 27 | "net/http/httptest"
|
| 28 | + "strings" |
26 | 29 | "testing"
|
27 | 30 |
|
28 | 31 | "github.com/orca-group/spirit/internal/util"
|
@@ -51,9 +54,42 @@ func TestCountLines(t *testing.T) {
|
51 | 54 | require.Equal(t, lines, template.HTML("<div>1</div><div>2</div>"))
|
52 | 55 | }
|
53 | 56 |
|
54 |
| -func TestHandleBodyDetection(t *testing.T) {} |
55 |
| -func TestHandleBodyJSON(t *testing.T) {} |
56 |
| -func TestHandleBodyMultipart(t *testing.T) {} |
| 57 | +func TestHandleBodyJSON(t *testing.T) { |
| 58 | + var buf bytes.Buffer |
| 59 | + json.NewEncoder(&buf).Encode(map[string]interface{}{ |
| 60 | + "content": "Hello, world!", |
| 61 | + }) |
| 62 | + |
| 63 | + req := httptest.NewRequest(http.MethodPost, "/", &buf) |
| 64 | + req.Header.Set("Content-Type", "application/json") |
| 65 | + body, err := util.HandleBody(400000, req) |
| 66 | + |
| 67 | + require.NoError(t, err) |
| 68 | + require.Equal(t, "Hello, world!", body.Content) |
| 69 | +} |
| 70 | + |
| 71 | +func TestHandleBodyMultipart(t *testing.T) { |
| 72 | + var buf bytes.Buffer |
| 73 | + writer := multipart.NewWriter(&buf) |
| 74 | + fw, _ := writer.CreateFormField("content") |
| 75 | + io.Copy(fw, strings.NewReader("Hello, world!")) |
| 76 | + writer.Close() |
| 77 | + |
| 78 | + req := httptest.NewRequest(http.MethodPost, "/", &buf) |
| 79 | + req.Header.Set("Content-Type", writer.FormDataContentType()) |
| 80 | + body, err := util.HandleBody(400000, req) |
| 81 | + |
| 82 | + require.NoError(t, err) |
| 83 | + require.Equal(t, "Hello, world!", body.Content) |
| 84 | +} |
| 85 | + |
| 86 | +func TestHandleBodyNoContent(t *testing.T) { |
| 87 | + req := httptest.NewRequest(http.MethodPost, "/", &bytes.Buffer{}) |
| 88 | + body, err := util.HandleBody(400000, req) |
| 89 | + |
| 90 | + require.NoError(t, err) |
| 91 | + require.Equal(t, "", body.Content) |
| 92 | +} |
57 | 93 |
|
58 | 94 | func TestWriteJSON(t *testing.T) {
|
59 | 95 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
0 commit comments