Skip to content

Commit

Permalink
Merge pull request #36 from ikawaha/develop
Browse files Browse the repository at this point in the history
Release candidate
  • Loading branch information
ikawaha authored Mar 21, 2023
2 parents 5932e9e + a148601 commit 9fd6c6f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linters:
- structcheck
- deadcode
- varcheck
- musttag
presets:
- bugs
- unused
Expand Down
Binary file added testdata/neko_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions tester_multipart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package httpcheck

import (
"bytes"
"fmt"
"mime/multipart"
"os"

"github.com/stretchr/testify/require"
)

// Parter is the interface that create a multipart part.
type Parter interface {
Part(mw *multipart.Writer) error
}

// FieldPart represents a multipart part of the field type part.
type FieldPart struct {
FieldName string
Value string
}

// Part returns a multipart part.
func (p FieldPart) Part(mw *multipart.Writer) error {
w, err := mw.CreateFormField(p.FieldName)
if err != nil {
return fmt.Errorf("failed to creat form field: %w", err)
}
if _, err := w.Write([]byte(p.Value)); err != nil {
return fmt.Errorf("write error: %w", err)
}
return nil
}

// FilePart represents a multipart part of the file type part.
type FilePart struct {
FieldName string
FileName string
}

// Part returns a multipart part.
func (p FilePart) Part(mw *multipart.Writer) error {
w, err := mw.CreateFormFile(p.FieldName, p.FileName)
if err != nil {
return fmt.Errorf("failed to create form file: %w", err)
}
b, err := os.ReadFile(p.FileName)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
if _, err := w.Write(b); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
return nil
}

// WithMultipart add a multipart data to the body.
func (tt *Tester) WithMultipart(part Parter, parts ...Parter) *Tester {
var b bytes.Buffer
mw := multipart.NewWriter(&b)
require.NoError(tt.t, part.Part(mw))
for _, v := range parts {
require.NoError(tt.t, v.Part(mw))
}
require.NoError(tt.t, mw.Close())
return tt.WithHeader("Content-Type", mw.FormDataContentType()).WithBody(b.Bytes())
}
54 changes: 54 additions & 0 deletions tester_multipart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package httpcheck

import (
"io"
"mime"
"mime/multipart"
"net/http"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTester_WithMultipart(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/multipart", func(w http.ResponseWriter, r *http.Request) {
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
require.NoError(t, err)
assert.Equal(t, "multipart/form-data", mediaType)
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(r.Body, params["boundary"])
for {
p, err := mr.NextPart()
if err != nil {
assert.ErrorIs(t, err, io.EOF)
break
}
got, err := io.ReadAll(p)
require.NoError(t, err)
switch n := p.FormName(); n {
case "part_1":
assert.Equal(t, "part_1", p.FormName())
assert.Equal(t, "value_1", string(got))
case "part_2":
assert.Equal(t, "neko_small.png", p.FileName())
b, err := os.ReadFile("./testdata/neko_small.png")
require.NoError(t, err)
assert.Equal(t, b, got)
}
p.Close()
}
}
w.WriteHeader(http.StatusOK)
})
checker := New(mux)
checker.Test(t, "POST", "/multipart").
WithMultipart(
FieldPart{FieldName: "part_1", Value: "value_1"},
FilePart{FieldName: "part_2", FileName: "./testdata/neko_small.png"},
).
Check().HasStatus(http.StatusOK)
}

0 comments on commit 9fd6c6f

Please sign in to comment.