|
| 1 | +package httprequest_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "mime/multipart" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "strconv" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/mutablelogic/go-server/pkg/httprequest" |
| 14 | + "github.com/mutablelogic/go-server/pkg/httpresponse" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + |
| 17 | + // Namespace imports |
| 18 | + . "github.com/djthorpe/go-errors" |
| 19 | +) |
| 20 | + |
| 21 | +func Test_body_00(t *testing.T) { |
| 22 | + assert := assert.New(t) |
| 23 | + |
| 24 | + t.Run("EOF", func(t *testing.T) { |
| 25 | + // We should receive an EOF error here - no body |
| 26 | + req := httptest.NewRequest("GET", "/", nil) |
| 27 | + req.Header.Set("Content-Type", "application/json") |
| 28 | + err := httprequest.Body(nil, req) |
| 29 | + assert.ErrorIs(err, io.EOF) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("ContentType1", func(t *testing.T) { |
| 33 | + // We should receive a badparameter error - invalid content type |
| 34 | + req := httptest.NewRequest("GET", "/", nil) |
| 35 | + req.Header.Set("Content-Type", "application/test") |
| 36 | + err := httprequest.Body(nil, req) |
| 37 | + assert.ErrorIs(err, ErrBadParameter) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("ContentType2", func(t *testing.T) { |
| 41 | + // We should receive a badparameter error - invalid content type |
| 42 | + req := httptest.NewRequest("GET", "/", nil) |
| 43 | + req.Header.Set("Content-Type", "text/plain") |
| 44 | + err := httprequest.Body(nil, req, httpresponse.ContentTypeJSON) |
| 45 | + assert.ErrorIs(err, ErrBadParameter) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("Writer", func(t *testing.T) { |
| 49 | + var in, out bytes.Buffer |
| 50 | + in.WriteString("Hello, World!") |
| 51 | + |
| 52 | + // We should receive the text |
| 53 | + req := httptest.NewRequest("GET", "/", &in) |
| 54 | + req.Header.Set("Content-Type", "text/plain") |
| 55 | + err := httprequest.Body(&out, req) |
| 56 | + assert.NoError(err) |
| 57 | + assert.Equal("Hello, World!", out.String()) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("Text", func(t *testing.T) { |
| 61 | + var in bytes.Buffer |
| 62 | + var out string |
| 63 | + in.WriteString("Hello, World!") |
| 64 | + |
| 65 | + // We should receive the text |
| 66 | + req := httptest.NewRequest("GET", "/", &in) |
| 67 | + req.Header.Set("Content-Type", "text/plain") |
| 68 | + err := httprequest.Body(&out, req) |
| 69 | + assert.NoError(err) |
| 70 | + assert.Equal("Hello, World!", out) |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("JSON", func(t *testing.T) { |
| 74 | + var in bytes.Buffer |
| 75 | + var out string |
| 76 | + in.WriteString(strconv.Quote("Hello, World!")) |
| 77 | + |
| 78 | + // We should receive the JSON and decode it |
| 79 | + req := httptest.NewRequest("GET", "/", &in) |
| 80 | + req.Header.Set("Content-Type", "application/json") |
| 81 | + err := httprequest.Body(&out, req) |
| 82 | + assert.NoError(err) |
| 83 | + assert.Equal("Hello, World!", out) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("Form", func(t *testing.T) { |
| 87 | + values := url.Values{ |
| 88 | + "key1": []string{"Hello, World!"}, |
| 89 | + } |
| 90 | + var in bytes.Buffer |
| 91 | + var out struct { |
| 92 | + Key1 string `json:"key1"` |
| 93 | + } |
| 94 | + in.WriteString(values.Encode()) |
| 95 | + |
| 96 | + // POST form data |
| 97 | + req := httptest.NewRequest("POST", "/", &in) |
| 98 | + req.Header.Set("Content-Type", httprequest.ContentTypeUrlEncoded) |
| 99 | + err := httprequest.Body(&out, req) |
| 100 | + assert.NoError(err) |
| 101 | + assert.Equal("Hello, World!", out.Key1) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("File", func(t *testing.T) { |
| 105 | + var in bytes.Buffer |
| 106 | + var out struct { |
| 107 | + File *multipart.FileHeader `json:"file"` |
| 108 | + } |
| 109 | + |
| 110 | + // instantiate multipart request |
| 111 | + multipartWriter := multipart.NewWriter(&in) |
| 112 | + |
| 113 | + // add form field |
| 114 | + filePart, _ := multipartWriter.CreateFormFile("file", "file.txt") |
| 115 | + filePart.Write([]byte("Hello, World!")) |
| 116 | + |
| 117 | + // Close the body - so it can be read |
| 118 | + multipartWriter.Close() |
| 119 | + |
| 120 | + // Create the request |
| 121 | + req := httptest.NewRequest(http.MethodPost, "/file", &in) |
| 122 | + req.Header.Set("Content-Type", multipartWriter.FormDataContentType()) |
| 123 | + |
| 124 | + // Parse the request body |
| 125 | + err := httprequest.Body(&out, req) |
| 126 | + assert.NoError(err) |
| 127 | + assert.NotNil(out.File) |
| 128 | + |
| 129 | + // Let's read the contents of the file |
| 130 | + file, err := out.File.Open() |
| 131 | + assert.NoError(err) |
| 132 | + defer file.Close() |
| 133 | + |
| 134 | + // Read the contents of the file |
| 135 | + data, err := io.ReadAll(file) |
| 136 | + assert.NoError(err) |
| 137 | + assert.Equal("Hello, World!", string(data)) |
| 138 | + }) |
| 139 | +} |
0 commit comments