-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To upload file with test request you need to specify names and paths of files. Don't forget specify Content-Type: > Content-Type: multipart/form-data Example: - name: "upload-files" method: POST form: files: file1: "testdata/upload-files/file1.txt" file2: "testdata/upload-files/file2.log" headers: # case-sensitive, can be omitted Content-Type: multipart/form-data response: 200: | { "status": "OK" }
- Loading branch information
Showing
11 changed files
with
331 additions
and
13 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
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
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
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
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,66 @@ | ||
package runner | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUploadFiles(t *testing.T) { | ||
srv := testServerUpload(t) | ||
defer srv.Close() | ||
|
||
// TODO: refactor RunWithTesting() for testing negative scenario (when tests has expected errors) | ||
RunWithTesting(t, &RunWithTestingParams{ | ||
Server: srv, | ||
TestsDir: filepath.Join("testdata", "upload-files"), | ||
}) | ||
} | ||
|
||
type response struct { | ||
Status string `json:"status"` | ||
File1Name string `json:"file_1_name"` | ||
File1Content string `json:"file_1_content"` | ||
File2Name string `json:"file_2_name"` | ||
File2Content string `json:"file_2_content"` | ||
} | ||
|
||
func testServerUpload(t *testing.T) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
resp := response{ | ||
Status: "OK", | ||
} | ||
|
||
resp.File1Name, resp.File1Content = formFile(t, r, "file1") | ||
resp.File2Name, resp.File2Content = formFile(t, r, "file2") | ||
|
||
respData, err := json.Marshal(resp) | ||
require.NoError(t, err) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
_, err = w.Write(respData) | ||
require.NoError(t, err) | ||
|
||
return | ||
})) | ||
} | ||
|
||
func formFile(t *testing.T, r *http.Request, field string) (string, string) { | ||
|
||
file, header, err := r.FormFile(field) | ||
require.NoError(t, err) | ||
|
||
defer func() { _ = file.Close() }() | ||
|
||
contents, err := ioutil.ReadAll(file) | ||
require.NoError(t, err) | ||
|
||
return header.Filename, string(contents) | ||
} |
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 @@ | ||
file1_some_text |
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 @@ | ||
file2_content |
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,35 @@ | ||
- name: "upload-files: with correct Content-Type" | ||
method: POST | ||
form: | ||
files: | ||
file1: "testdata/upload-files/file1.txt" | ||
file2: "testdata/upload-files/file2.log" | ||
headers: | ||
Content-Type: multipart/form-data | ||
response: | ||
200: | | ||
{ | ||
"status": "OK", | ||
"file_1_name": "file1.txt", | ||
"file_1_content": "file1_some_text", | ||
"file_2_name": "file2.log", | ||
"file_2_content": "file2_content" | ||
} | ||
- name: "upload-files: with empty Content-Type" | ||
method: POST | ||
form: | ||
files: | ||
file1: "testdata/upload-files/file1.txt" | ||
file2: "testdata/upload-files/file2.log" | ||
response: | ||
200: | | ||
{ | ||
"status": "OK", | ||
"file_1_name": "file1.txt", | ||
"file_1_content": "file1_some_text", | ||
"file_2_name": "file2.log", | ||
"file_2_content": "file2_content" | ||
} | ||
# TODO: test with incorrect Content-Type |
Oops, something went wrong.