Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
213 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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package upload | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"mime/multipart" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type GraphQLRequestFile struct { | ||
Filename string `json:"filename"` | ||
Filepath string `json:"filepath"` | ||
MIMEType string `json:"mimetype"` | ||
} | ||
|
||
type GraphQLRequestVariables struct { | ||
File GraphQLRequestFile `json:"file"` | ||
Title string `json:"title"` | ||
} | ||
|
||
type GraphQLRequest struct { | ||
Variables GraphQLRequestVariables `json:"variables"` | ||
} | ||
|
||
func openTestTempFile(filename string) *os.File { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return file | ||
} | ||
|
||
func TestHandlerWhenSuccess(t *testing.T) { | ||
// Given | ||
filePath, _ := os.Getwd() | ||
filePath += "/middleware.go" | ||
|
||
file := openTestTempFile(filePath) | ||
defer file.Close() | ||
|
||
var requestBody bytes.Buffer | ||
writer := multipart.NewWriter(&requestBody) | ||
|
||
fw, err := writer.CreateFormFile("file", "middleware.go") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if _, err = io.Copy(fw, file); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Add the other fields | ||
_ = writer.WriteField("operations", "{ \"query\": \"mutation DoUpload($file: Upload!, $title: String) { upload(file: $file, title: $title) { code } }\", \"variables\": { \"file\": null, \"title\": \"my test title\" } }") | ||
_ = writer.WriteField("map", "{ \"file\": [\"variables.file\"], \"title\": [\"variables.title\"] }") | ||
_ = writer.WriteField("title", "my test title") | ||
|
||
writer.Close() | ||
|
||
req, _ := http.NewRequest("POST", "/test", &requestBody) | ||
req.Header.Add("Content-Type", writer.FormDataContentType()) | ||
|
||
w := httptest.NewRecorder() | ||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Do nothing, this is a fake HTTP request handler: goal is to test our upload middleware. | ||
}) | ||
|
||
// When | ||
nextMidware := Handler(next) | ||
nextMidware.ServeHTTP(w, req) | ||
|
||
// Then | ||
body, _ := ioutil.ReadAll(req.Body) | ||
|
||
var data GraphQLRequest | ||
if err := json.Unmarshal(body, &data); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Should render GraphQL variables entry containing parameters and file data. | ||
// Also, the content-type of the request have to be switched to "application/json". | ||
assert.Equal(t, w.Header().Get("Content-Type"), "application/json") | ||
|
||
assert.NotEmpty(t, data.Variables.File.Filepath) | ||
assert.Equal(t, data.Variables.File.Filename, "middleware.go") | ||
assert.Equal(t, data.Variables.File.MIMEType, "text/plain; charset=utf-8") | ||
|
||
assert.Equal(t, data.Variables.Title, "my test title") | ||
} | ||
|
||
func TestIsMiddlewareSupportedWhenSupported(t *testing.T) { | ||
// Given | ||
req, _ := http.NewRequest("POST", "/test", nil) | ||
req.Header.Add("Content-Type", "multipart/form-data") | ||
|
||
// When | ||
isSupported := isMiddlewareSupported(req) | ||
|
||
// Then | ||
assert.True(t, isSupported) | ||
} | ||
|
||
func TestIsMiddlewareSupportedWhenNotSupported(t *testing.T) { | ||
// Given | ||
req, _ := http.NewRequest("POST", "/test", nil) | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
// When | ||
isSupported := isMiddlewareSupported(req) | ||
|
||
// Then | ||
assert.False(t, isSupported) | ||
} |
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,92 @@ | ||
package upload | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"os" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestImplementsGraphQLType(t *testing.T) { | ||
// Given | ||
upload := GraphQLUpload{} | ||
|
||
// When - Then | ||
assert.True(t, upload.ImplementsGraphQLType("Upload")) | ||
assert.False(t, upload.ImplementsGraphQLType("AnotherThing")) | ||
} | ||
|
||
func TestUnmarshalGraphQLWhenMapString(t *testing.T) { | ||
// Given | ||
upload := GraphQLUpload{} | ||
|
||
input := map[string]interface{}{ | ||
"filename": "my-test.jpg", | ||
"mimetype": "image/jpeg", | ||
"filepath": "/tmp/my-test.jpg", | ||
} | ||
|
||
// When | ||
err := upload.UnmarshalGraphQL(input) | ||
|
||
// Then | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, upload.Filename, input["filename"]) | ||
assert.Equal(t, upload.MIMEType, input["mimetype"]) | ||
assert.Equal(t, upload.Filepath, input["filepath"]) | ||
} | ||
|
||
func TestUnmarshalGraphQLWhenAnotherType(t *testing.T) { | ||
// Given | ||
upload := GraphQLUpload{} | ||
|
||
input := map[int]interface{}{ | ||
1: "my-test.jpg", | ||
2: "image/jpeg", | ||
3: "/tmp/my-test.jpg", | ||
} | ||
|
||
// When | ||
err := upload.UnmarshalGraphQL(input) | ||
|
||
// Then | ||
assert.Equal(t, "Cannot unmarshal received type as a GraphQLUpload type", err.Error()) | ||
} | ||
|
||
func TestGetReaderWhenFileExists(t *testing.T) { | ||
// Given | ||
f, err := ioutil.TempFile("", "testfile") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer syscall.Unlink(f.Name()) | ||
|
||
upload := GraphQLUpload{ | ||
Filepath: f.Name(), | ||
} | ||
|
||
// When | ||
reader, err := upload.GetReader() | ||
|
||
// Then | ||
assert.IsType(t, new(bufio.Reader), reader) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestGetReaderWhenFileDoesNotExists(t *testing.T) { | ||
// Given | ||
upload := GraphQLUpload{ | ||
Filepath: "unknown-file.jpeg", | ||
} | ||
|
||
// When | ||
reader, err := upload.GetReader() | ||
|
||
// Then | ||
assert.Nil(t, reader) | ||
assert.IsType(t, new(os.PathError), err) | ||
} |