| 51 | } |
| 52 | |
| 53 | func TestReadJSONFromMIME(t *testing.T) { |
| 54 | // Read JSON from MIME by specifying non-JSON content type |
| 55 | header := http.Header{} |
| 56 | header.Add("MIME-Version", "1.0") |
| 57 | header.Add("Content-Type", "multipart/related; boundary=0123456789") |
| 58 | input := io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`)) |
| 59 | var body db.Body |
| 60 | err := ReadJSONFromMIME(header, input, &body) |
| 61 | assert.Error(t, err, "Can't read JSON from MIME by specifying non-JSON content type") |
| 62 | assert.Contains(t, err.Error(), strconv.Itoa(http.StatusUnsupportedMediaType)) |
| 63 | |
| 64 | // Read valid JSON from MIME |
| 65 | header = http.Header{} |
| 66 | header.Add("MIME-Version", "1.0") |
| 67 | header.Add("Content-Type", "application/json") |
| 68 | input = io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`)) |
| 69 | err = ReadJSONFromMIME(header, input, &body) |
| 70 | assert.NoError(t, err, "Should read valid JSON from MIME") |
| 71 | assert.Equal(t, "foo", body["key"]) |
| 72 | assert.Equal(t, "bar", body["value"]) |
| 73 | |
| 74 | // Read JSON from MIME with illegal JSON body content |
| 75 | header = http.Header{} |
| 76 | header.Add("MIME-Version", "1.0") |
| 77 | header.Add("Content-Type", "application/json") |
| 78 | input = io.NopCloser(strings.NewReader(`"key":"foo","value":"bar"`)) |
| 79 | err = ReadJSONFromMIME(header, input, &body) |
| 80 | assert.Error(t, err, "Can't read JSON from MIME with illegal JSON body content") |
| 81 | assert.Contains(t, err.Error(), strconv.Itoa(http.StatusBadRequest)) |
| 82 | |
| 83 | // Read JSON from MIME with gzip content encoding and illegal content type (application/json) |
| 84 | header = http.Header{} |
| 85 | header.Add("MIME-Version", "1.0") |
| 86 | header.Add("Content-Type", "application/json") |
| 87 | header.Add("Content-Encoding", "gzip") |
| 88 | input = io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`)) |
| 89 | err = ReadJSONFromMIME(header, input, &body) |
| 90 | assert.Error(t, err, "Can't read JSON from MIME with gzip content encoding and illegal content type") |
| 91 | assert.Contains(t, err.Error(), "invalid header") |
| 92 | |
| 93 | // Read JSON from MIME with unsupported content encoding. |
| 94 | header = http.Header{} |
| 95 | header.Add("MIME-Version", "1.0") |
| 96 | header.Add("Content-Encoding", "zip") |
| 97 | input = io.NopCloser(strings.NewReader(`{"key":"foo","value":"bar"}`)) |
| 98 | err = ReadJSONFromMIME(header, input, &body) |
| 99 | assert.Error(t, err, "Can't read JSON from MIME with unsupported content encoding") |
| 100 | assert.Contains(t, err.Error(), strconv.Itoa(http.StatusUnsupportedMediaType)) |
| 101 | |
| 102 | // Read JSON from MIME with gzip content encoding. |
| 103 | header = http.Header{} |
| 104 | header.Add("MIME-Version", "1.0") |
| 105 | header.Add("Content-Encoding", "gzip") |
| 106 | var buffer bytes.Buffer |
| 107 | |
| 108 | gz := gzip.NewWriter(&buffer) |
| 109 | _, err = gz.Write([]byte(`{"key":"foo","value":"bar"}`)) |
| 110 | assert.NoError(t, err, "Writes a compressed form of bytes to the underlying io.Writer") |