| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "image" |
| 7 | "image/color" |
| 8 | "image/png" |
| 9 | "mime/multipart" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "net/url" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | // The images' outside helpers: a picture, an upload through POST |
| 17 | // /images, and a copy through POST /images/copy. |
| 18 | |
| 19 | // pngOf is a small PNG, different for each seed. |
| 20 | func pngOf(t *testing.T, seed int) []byte { |
| 21 | t.Helper() |
| 22 | canvas := image.NewRGBA(image.Rect(0, 0, 24, 16)) |
| 23 | for column := range 24 { |
| 24 | for line := range 16 { |
| 25 | canvas.Set(column, line, color.RGBA{uint8(column * seed), |
| 26 | uint8(line * 9), uint8(seed), 255}) |
| 27 | } |
| 28 | } |
| 29 | var out bytes.Buffer |
| 30 | if err := png.Encode(&out, canvas); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | return out.Bytes() |
| 34 | } |
| 35 | |
| 36 | // multipartOf is a multipart body: fields, then the file. |
| 37 | func multipartOf(t *testing.T, fields url.Values, name string, |
| 38 | file []byte) (*bytes.Buffer, string) { |
| 39 | t.Helper() |
| 40 | var body bytes.Buffer |
| 41 | writer := multipart.NewWriter(&body) |
| 42 | for key, values := range fields { |
| 43 | for _, value := range values { |
| 44 | _ = writer.WriteField(key, value) |
| 45 | } |
| 46 | } |
| 47 | part, err := writer.CreateFormFile("file", name) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | _, _ = part.Write(file) |
| 52 | if err := writer.Close(); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | return &body, writer.FormDataContentType() |
| 56 | } |
| 57 | |
| 58 | // sendFile posts a multipart request. |
| 59 | func (rig *rig) sendFile(t *testing.T, who, target string, |
| 60 | fields url.Values, name string, |
| 61 | file []byte) *httptest.ResponseRecorder { |
| 62 | t.Helper() |
| 63 | body, kind := multipartOf(t, fields, name, file) |
| 64 | request := httptest.NewRequest(http.MethodPost, target, body) |
| 65 | request.Header.Set("Content-Type", kind) |
| 66 | request.Header.Set("Accept", "application/json") |
| 67 | request.Header.Set("Origin", testOrigin) |
| 68 | recorder := httptest.NewRecorder() |
| 69 | rig.built.As(who).ServeHTTP(recorder, request) |
| 70 | return recorder |
| 71 | } |
| 72 | |
| 73 | // uploaded is one upload's reply. |
| 74 | type uploaded struct { |
| 75 | ID string `json:"id"` |
| 76 | Src string `json:"src"` |
| 77 | Width int `json:"width"` |
| 78 | Height int `json:"height"` |
| 79 | } |
| 80 | |
| 81 | // upload sends raw to POST /images as who and answers its reply. |
| 82 | func (rig *rig) upload(t *testing.T, who string, raw []byte) uploaded { |
| 83 | t.Helper() |
| 84 | reply := rig.sendFile(t, who, "/images", nil, "photo.png", raw) |
| 85 | if reply.Code != http.StatusOK { |
| 86 | t.Fatalf("upload: %d %s", reply.Code, reply.Body.String()) |
| 87 | } |
| 88 | var out uploaded |
| 89 | if err := json.Unmarshal(reply.Body.Bytes(), &out); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | return out |
| 93 | } |
| 94 | |
| 95 | func (image uploaded) markdown(alt string) string { |
| 96 | return "" |
| 97 | } |
| 98 | |