-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patheditor_test.go
190 lines (175 loc) · 5.37 KB
/
editor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestEditorHandleFileAttachments(t *testing.T) {
a := &goBlog{
cfg: createDefaultTestConfig(t),
}
_ = a.initConfig(false)
// Helper function to create a multipart form request
createMultipartRequest := func(files map[string]map[string]string) *http.Request {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for fieldName, files := range files {
for fileName, fileContent := range files {
part, err := writer.CreateFormFile(fieldName, fileName)
if err != nil {
t.Fatalf("Failed to create form file: %v", err)
}
_, err = io.Copy(part, strings.NewReader(fileContent))
if err != nil {
t.Fatalf("Failed to copy file content: %v", err)
}
}
}
writer.Close()
req := httptest.NewRequest(http.MethodPost, "/", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req
}
t.Run("Valid GPX file", func(t *testing.T) {
req := createMultipartRequest(map[string]map[string]string{
"files1": {
"track.gpx": "<gpx>valid gpx content</gpx>",
},
})
images, gpx, statusCode, err := a.editorHandleFileAttachments(req)
if err != nil || statusCode != 0 {
t.Fatalf("Expected no error, got %v with status code %d", err, statusCode)
}
if len(images) != 0 {
t.Fatalf("Expected no images, got %d", len(images))
}
if gpx == "" {
t.Fatal("Expected GPX content, got empty string")
}
})
t.Run("Invalid GPX file", func(t *testing.T) {
req := createMultipartRequest(map[string]map[string]string{
"files1": {
"track.gpx": "invalid gpx content",
},
})
_, _, statusCode, err := a.editorHandleFileAttachments(req)
if err == nil || statusCode != http.StatusBadRequest {
t.Fatalf("Expected error with status code %d, got %v with status code %d", http.StatusBadRequest, err, statusCode)
}
})
t.Run("Multiple files including GPX", func(t *testing.T) {
req := createMultipartRequest(map[string]map[string]string{
"files1": {
"image1.jpg": "image content",
"image2.png": "image content",
"track.gpx": "<gpx>valid gpx content</gpx>",
},
"files2": {
"image3.jpg": "image content",
},
})
images, gpx, statusCode, err := a.editorHandleFileAttachments(req)
if err != nil || statusCode != 0 {
t.Fatalf("Expected no error, got %v with status code %d", err, statusCode)
}
if len(images) != 3 {
t.Fatalf("Expected 3 image, got %d", len(images))
}
if gpx == "" {
t.Fatal("Expected GPX content, got empty string")
}
})
t.Run("No files", func(t *testing.T) {
req := createMultipartRequest(map[string]map[string]string{})
images, gpx, statusCode, err := a.editorHandleFileAttachments(req)
if err != nil || statusCode != 0 {
t.Fatalf("Expected no error, got %v with status code %d", err, statusCode)
}
if len(images) != 0 {
t.Fatalf("Expected no images, got %d", len(images))
}
if gpx != "" {
t.Fatalf("Expected no GPX content, got %s", gpx)
}
})
}
func TestServeEditorPost(t *testing.T) {
a := &goBlog{
cfg: createDefaultTestConfig(t),
}
_ = a.initConfig(false)
t.Run("Unknown action", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
req.Form = url.Values{"editoraction": {"abc"}}
rec := httptest.NewRecorder()
a.serveEditorPost(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("Expected status %d, got %d", http.StatusBadRequest, rec.Code)
}
if !strings.Contains(rec.Body.String(), "Unknown or missing editoraction") {
t.Fatalf("Expected error message, got %s", rec.Body.String())
}
})
t.Run("Help GPX with valid file", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("files", "track1.gpx")
_, _ = part.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="ExampleCreator">
<trk>
<name>Example Track</name>
<trkseg>
<trkpt lat="48.208174" lon="16.373819">
<ele>160</ele>
<time>2023-03-01T12:00:00Z</time>
</trkpt>
<trkpt lat="48.208255" lon="16.374123">
<ele>162</ele>
<time>2023-03-01T12:01:00Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>`))
part, _ = writer.CreateFormFile("files", "track2.gpx")
_, _ = part.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="AnotherExampleCreator">
<trk>
<name>Another Example Track</name>
<trkseg>
<trkpt lat="40.712776" lon="-74.005974">
<ele>10</ele>
<time>2023-03-02T08:00:00Z</time>
</trkpt>
<trkpt lat="40.713776" lon="-74.006974">
<ele>12</ele>
<time>2023-03-02T08:01:00Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>`))
writer.Close()
req := httptest.NewRequest(http.MethodPost, "/", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Form = url.Values{"editoraction": {"helpgpx"}}
rec := httptest.NewRecorder()
a.serveEditorPost(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("Expected status %d, got %d", http.StatusOK, rec.Code)
}
if !strings.Contains(rec.Body.String(), "gpx") {
t.Fatalf("Expected GPX response, got %s", rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "48.208174") {
t.Fatalf("Expected to include first file points, got %s", rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "40.712776") {
t.Fatalf("Expected to include second file points, got %s", rec.Body.String())
}
})
}