Skip to content

Commit

Permalink
test(helpers): body parsing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewhrit committed Jun 23, 2024
1 parent fd6025a commit a159de6
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions internal/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
package util_test

import (
"bytes"
"encoding/json"
"errors"
"html/template"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/orca-group/spirit/internal/util"
Expand Down Expand Up @@ -51,9 +54,42 @@ func TestCountLines(t *testing.T) {
require.Equal(t, lines, template.HTML("<div>1</div><div>2</div>"))
}

func TestHandleBodyDetection(t *testing.T) {}
func TestHandleBodyJSON(t *testing.T) {}
func TestHandleBodyMultipart(t *testing.T) {}
func TestHandleBodyJSON(t *testing.T) {
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(map[string]interface{}{
"content": "Hello, world!",
})

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Type", "application/json")
body, err := util.HandleBody(400000, req)

require.NoError(t, err)
require.Equal(t, "Hello, world!", body.Content)
}

func TestHandleBodyMultipart(t *testing.T) {
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
fw, _ := writer.CreateFormField("content")
io.Copy(fw, strings.NewReader("Hello, world!"))
writer.Close()

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Type", writer.FormDataContentType())
body, err := util.HandleBody(400000, req)

require.NoError(t, err)
require.Equal(t, "Hello, world!", body.Content)
}

func TestHandleBodyNoContent(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", &bytes.Buffer{})
body, err := util.HandleBody(400000, req)

require.NoError(t, err)
require.Equal(t, "", body.Content)
}

func TestWriteJSON(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit a159de6

Please sign in to comment.