forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ikawaha/develop
Release candidate
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ linters: | |
- structcheck | ||
- deadcode | ||
- varcheck | ||
- musttag | ||
presets: | ||
- bugs | ||
- unused | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |