-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
geocode_test.go
76 lines (68 loc) · 2.14 KB
/
geocode_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/saidsef/faas-reverse-geocoding/internal/handlers"
)
// TestLatitudeLongitudeHandler tests the latitudeLongitude HTTP handler function.
func TestLatitudeLongitudeHandler(t *testing.T) {
// Test cases to cover all critical paths.
tests := []struct {
name string
method string
body string
expectedStatus int
expectedBody string
}{
{
name: "Health Check - GET Request",
method: "GET",
expectedStatus: http.StatusOK,
expectedBody: `{"status": "healthy"}`,
},
{
name: "POST Request - Missing Body",
method: "POST",
expectedStatus: http.StatusBadRequest,
expectedBody: "EOF\n",
},
{
name: "POST Request - Incomplete Data",
method: "POST",
body: `{"lat": "51.5074"}`,
expectedStatus: http.StatusBadRequest,
expectedBody: "strconv.ParseFloat: parsing \"\": invalid syntax\n",
},
// Add more test cases as needed, especially for successful POST requests.
// Note: Successful POST requests would require mocking the external API call to Nominatim.
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a request with the specified method, URL, and body.
var req *http.Request
if tc.body != "" {
req = httptest.NewRequest(tc.method, "/", bytes.NewBufferString(tc.body))
} else {
req = httptest.NewRequest(tc.method, "/", nil)
}
// Record the response.
rr := httptest.NewRecorder()
// Create an HTTP handler from our function.
handler := http.HandlerFunc(handlers.LatitudeLongitude)
// Serve the HTTP request to our handler.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != tc.expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tc.expectedStatus)
}
// Check the response body is what we expect.
if rr.Body.String() != tc.expectedBody {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), tc.expectedBody)
}
})
}
}