parse_integration_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package web
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "mime/multipart"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/gin-gonic/gin"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func init() {
  14. gin.SetMode(gin.TestMode)
  15. }
  16. func Test_parse(t *testing.T) {
  17. tests := []struct {
  18. name string
  19. body [][]byte
  20. expectedStatus int
  21. expectedReply string
  22. }{
  23. {name: "No files uploaded",
  24. body: [][]byte{},
  25. expectedStatus: 200,
  26. expectedReply: `[]`,
  27. },
  28. {name: "1 file uploaded and succeeds",
  29. 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}},
  30. expectedStatus: 200,
  31. expectedReply: "[{\"pokedex_number\":20,\"raw_nickname\":\"UgBhAHQAaQBjAGEAdABlAAAAAAAAAAAA\",\"nickname\":\"Raticate\",\"raw_pokemon\":\"ewMXgQAA3QEUAAAAbaoyREAfAAAvBAAAQ1aIAxISAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFIAYQB0AGkAYwBhAHQAZQAAAAAAAAAAAAAApAB1AVMBngAKDxQPAAAAAAAAAAAAAAAAAAA8/P8XUABQAG8AcgBnAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAARgAAAAAAAAAAAAAAAABQAFAAbwByAGcAAAAAAAAAAAAAAAAAAAAAAEYAAAAAAAAAAAARCxIAAADKAAQUACEBAgACAAAAAA==\"}]",
  32. },
  33. {name: "1 file uploaded and fails",
  34. body: [][]byte{{0x7b}},
  35. expectedStatus: 422,
  36. expectedReply: "{\"error\":\"Invalid length for generation 7 pokemon: 1 bytes\"}",
  37. },
  38. {name: "Multiple files uploaded and succeed",
  39. body: [][]byte{
  40. {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},
  41. {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},
  42. },
  43. expectedStatus: 200,
  44. 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==\"}]",
  45. },
  46. {name: "Multiple files uploaded and fail",
  47. body: [][]byte{
  48. {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},
  49. {0x7b, 0x03},
  50. },
  51. expectedStatus: 422,
  52. expectedReply: "{\"error\":\"Invalid length for generation 7 pokemon: 2 bytes\"}",
  53. },
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. s := &Server{}
  58. rec := httptest.NewRecorder()
  59. ctx, _ := gin.CreateTestContext(rec)
  60. var b bytes.Buffer
  61. var err error
  62. var fw io.Writer
  63. w := multipart.NewWriter(&b)
  64. for i, postBody := range tt.body {
  65. if fw, err = w.CreateFormFile("pokemon", fmt.Sprintf("test%d.pk7", i+1)); err != nil {
  66. assert.Fail(t, "Failed to create form file: %s", err)
  67. return
  68. }
  69. body := bytes.NewBuffer(postBody)
  70. if _, err = io.Copy(fw, body); err != nil {
  71. assert.Fail(t, "Failed to copy buffer: %s", err)
  72. return
  73. }
  74. }
  75. w.Close()
  76. var req *http.Request
  77. if req, err = http.NewRequest(http.MethodPost, "/parse", &b); err != nil {
  78. assert.Fail(t, "Failed to create request: %s", err)
  79. return
  80. }
  81. req.Header.Set("Content-Type", w.FormDataContentType())
  82. ctx.Request = req
  83. s.parse(ctx)
  84. assert.Equal(t, tt.expectedReply, rec.Body.String())
  85. assert.Equal(t, tt.expectedStatus, ctx.Writer.Status())
  86. })
  87. }
  88. }
  89. // //prepare the reader instances to encode
  90. // values := map[string]io.Reader{
  91. // "file": mustOpen("main.go"), // lets assume its this file
  92. // "other": strings.NewReader("hello world!"),
  93. // }
  94. // err := Upload(client, remoteURL, values)
  95. // if err != nil {
  96. // panic(err)
  97. // }
  98. //}
  99. //func Upload(client *http.Client, url string, values map[string]io.Reader) (err error) {
  100. // // Prepare a form that you will submit to that URL.
  101. // var b bytes.Buffer
  102. // w := multipart.NewWriter(&b)
  103. // for key, r := range values {
  104. // var fw io.Writer
  105. // if x, ok := r.(io.Closer); ok {
  106. // defer x.Close()
  107. // }
  108. // // Add an image file
  109. // if x, ok := r.(*os.File); ok {
  110. // if fw, err = w.CreateFormFile(key, x.Name()); err != nil {
  111. // return
  112. // }
  113. // } else {
  114. // // Add other fields
  115. // if fw, err = w.CreateFormField(key); err != nil {
  116. // return
  117. // }
  118. // }
  119. // if _, err = io.Copy(fw, r); err != nil {
  120. // return err
  121. // }
  122. // }
  123. // // Don't forget to close the multipart writer.
  124. // // If you don't close it, your request will be missing the terminating boundary.
  125. // w.Close()
  126. // // Now that you have a form, you can submit it to your handler.
  127. // req, err := http.NewRequest("POST", url, &b)