package web import ( "bytes" "fmt" "io" "mime/multipart" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" ) func init() { gin.SetMode(gin.TestMode) } func Test_parse(t *testing.T) { tests := []struct { name string body [][]byte expectedStatus int expectedReply string }{ {name: "No files uploaded", body: [][]byte{}, expectedStatus: 200, expectedReply: `[]`, }, {name: "1 file uploaded and succeeds", body: [][]byte{{0x7b, 0x03, 0x17, 0x81, 0x00, 0x00, 0xdd, 0x01, 0x14, 0x00, 0x00, 0x00, 0x6d, 0xaa, 0x32, 0x44, 0x40, 0x1f, 0x00, 0x00, 0x2f, 0x04, 0x00, 0x00, 0x43, 0x56, 0x88, 0x03, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x63, 0x00, 0x61, 0x00, 0x74, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x75, 0x01, 0x53, 0x01, 0x9e, 0x00, 0x0a, 0x0f, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xfc, 0xff, 0x17, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x0b, 0x12, 0x00, 0x00, 0x00, 0xca, 0x00, 0x04, 0x14, 0x00, 0x21, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}}, expectedStatus: 200, expectedReply: "[{\"pokedex_number\":20,\"raw_nickname\":\"UgBhAHQAaQBjAGEAdABlAAAAAAAAAAAA\",\"nickname\":\"Raticate\",\"raw_pokemon\":\"ewMXgQAA3QEUAAAAbaoyREAfAAAvBAAAQ1aIAxISAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFIAYQB0AGkAYwBhAHQAZQAAAAAAAAAAAAAApAB1AVMBngAKDxQPAAAAAAAAAAAAAAAAAAA8/P8XUABQAG8AcgBnAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAARgAAAAAAAAAAAAAAAABQAFAAbwByAGcAAAAAAAAAAAAAAAAAAAAAAEYAAAAAAAAAAAARCxIAAADKAAQUACEBAgACAAAAAA==\"}]", }, {name: "1 file uploaded and fails", body: [][]byte{{0x7b}}, expectedStatus: 422, expectedReply: "{\"error\":\"Invalid length for generation 7 pokemon: 1 bytes\"}", }, {name: "Multiple files uploaded and succeed", body: [][]byte{ {0x7b, 0x03, 0x17, 0x81, 0x00, 0x00, 0xdd, 0x01, 0x14, 0x00, 0x00, 0x00, 0x6d, 0xaa, 0x32, 0x44, 0x40, 0x1f, 0x00, 0x00, 0x2f, 0x04, 0x00, 0x00, 0x43, 0x56, 0x88, 0x03, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x63, 0x00, 0x61, 0x00, 0x74, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x75, 0x01, 0x53, 0x01, 0x9e, 0x00, 0x0a, 0x0f, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xfc, 0xff, 0x17, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x0b, 0x12, 0x00, 0x00, 0x00, 0xca, 0x00, 0x04, 0x14, 0x00, 0x21, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, {0x7b, 0x03, 0x17, 0x81, 0x00, 0x00, 0xdd, 0x01, 0x14, 0x00, 0x00, 0x00, 0x6d, 0xaa, 0x32, 0x44, 0x40, 0x1f, 0x00, 0x00, 0x2f, 0x04, 0x00, 0x00, 0x43, 0x56, 0x88, 0x03, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x63, 0x00, 0x61, 0x00, 0x74, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x75, 0x01, 0x53, 0x01, 0x9e, 0x00, 0x0a, 0x0f, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xfc, 0xff, 0x17, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x0b, 0x12, 0x00, 0x00, 0x00, 0xca, 0x00, 0x04, 0x14, 0x00, 0x21, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, }, expectedStatus: 200, expectedReply: "[{\"pokedex_number\":20,\"raw_nickname\":\"UgBhAHQAaQBjAGEAdABlAAAAAAAAAAAA\",\"nickname\":\"Raticate\",\"raw_pokemon\":\"ewMXgQAA3QEUAAAAbaoyREAfAAAvBAAAQ1aIAxISAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFIAYQB0AGkAYwBhAHQAZQAAAAAAAAAAAAAApAB1AVMBngAKDxQPAAAAAAAAAAAAAAAAAAA8/P8XUABQAG8AcgBnAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAARgAAAAAAAAAAAAAAAABQAFAAbwByAGcAAAAAAAAAAAAAAAAAAAAAAEYAAAAAAAAAAAARCxIAAADKAAQUACEBAgACAAAAAA==\"},{\"pokedex_number\":20,\"raw_nickname\":\"UgBhAHQAaQBjAGEAdABmAAAAAAAAAAAA\",\"nickname\":\"Raticatf\",\"raw_pokemon\":\"ewMXgQAA3QEUAAAAbaoyREAfAAAvBAAAQ1aIAxISAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFIAYQB0AGkAYwBhAHQAZgAAAAAAAAAAAAAApAB1AVMBngAKDxQPAAAAAAAAAAAAAAAAAAA8/P8XUABQAG8AcgBnAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAARgAAAAAAAAAAAAAAAABQAFAAbwByAGcAAAAAAAAAAAAAAAAAAAAAAEYAAAAAAAAAAAARCxIAAADKAAQUACEBAgACAAAAAA==\"}]", }, {name: "Multiple files uploaded and fail", body: [][]byte{ {0x7b, 0x03, 0x17, 0x81, 0x00, 0x00, 0xdd, 0x01, 0x14, 0x00, 0x00, 0x00, 0x6d, 0xaa, 0x32, 0x44, 0x40, 0x1f, 0x00, 0x00, 0x2f, 0x04, 0x00, 0x00, 0x43, 0x56, 0x88, 0x03, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x00, 0x61, 0x00, 0x74, 0x00, 0x69, 0x00, 0x63, 0x00, 0x61, 0x00, 0x74, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x75, 0x01, 0x53, 0x01, 0x9e, 0x00, 0x0a, 0x0f, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xfc, 0xff, 0x17, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x50, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x0b, 0x12, 0x00, 0x00, 0x00, 0xca, 0x00, 0x04, 0x14, 0x00, 0x21, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, {0x7b, 0x03}, }, expectedStatus: 422, expectedReply: "{\"error\":\"Invalid length for generation 7 pokemon: 2 bytes\"}", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &Server{} rec := httptest.NewRecorder() ctx, _ := gin.CreateTestContext(rec) var b bytes.Buffer var err error var fw io.Writer w := multipart.NewWriter(&b) for i, postBody := range tt.body { if fw, err = w.CreateFormFile("pokemon", fmt.Sprintf("test%d.pk7", i+1)); err != nil { assert.Fail(t, "Failed to create form file: %s", err) return } body := bytes.NewBuffer(postBody) if _, err = io.Copy(fw, body); err != nil { assert.Fail(t, "Failed to copy buffer: %s", err) return } } w.Close() var req *http.Request if req, err = http.NewRequest(http.MethodPost, "/parse", &b); err != nil { assert.Fail(t, "Failed to create request: %s", err) return } req.Header.Set("Content-Type", w.FormDataContentType()) ctx.Request = req s.parse(ctx) assert.Equal(t, tt.expectedReply, rec.Body.String()) assert.Equal(t, tt.expectedStatus, ctx.Writer.Status()) }) } } // //prepare the reader instances to encode // values := map[string]io.Reader{ // "file": mustOpen("main.go"), // lets assume its this file // "other": strings.NewReader("hello world!"), // } // err := Upload(client, remoteURL, values) // if err != nil { // panic(err) // } //} //func Upload(client *http.Client, url string, values map[string]io.Reader) (err error) { // // Prepare a form that you will submit to that URL. // var b bytes.Buffer // w := multipart.NewWriter(&b) // for key, r := range values { // var fw io.Writer // if x, ok := r.(io.Closer); ok { // defer x.Close() // } // // Add an image file // if x, ok := r.(*os.File); ok { // if fw, err = w.CreateFormFile(key, x.Name()); err != nil { // return // } // } else { // // Add other fields // if fw, err = w.CreateFormField(key); err != nil { // return // } // } // if _, err = io.Copy(fw, r); err != nil { // return err // } // } // // Don't forget to close the multipart writer. // // If you don't close it, your request will be missing the terminating boundary. // w.Close() // // Now that you have a form, you can submit it to your handler. // req, err := http.NewRequest("POST", url, &b)