mirror of https://github.com/mpolden/echoip
http: Include status in response
This commit is contained in:
parent
86b68bdf55
commit
762f454865
|
@ -383,14 +383,15 @@ func wrapHandlerFunc(f http.HandlerFunc) appHandler {
|
|||
|
||||
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if e := fn(w, r); e != nil { // e is *appError
|
||||
// When Content-Type for error is JSON, we need to marshal the response into JSON
|
||||
if e.Code/100 == 5 {
|
||||
log.Println(e.Error)
|
||||
}
|
||||
// When Content-Type for error is JSON, we need to marshal the response into JSON
|
||||
if e.IsJSON() {
|
||||
var data = struct {
|
||||
Code int `json:"status"`
|
||||
Error string `json:"error"`
|
||||
}{e.Message}
|
||||
}{e.Code, e.Message}
|
||||
b, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
|
@ -154,14 +154,13 @@ func TestJSONHandlers(t *testing.T) {
|
|||
status int
|
||||
}{
|
||||
{s.URL, "{\n \"ip\": \"127.0.0.1\",\n \"ip_decimal\": 2130706433,\n \"country\": \"Elbonia\",\n \"country_iso\": \"EB\",\n \"country_eu\": false,\n \"region_name\": \"North Elbonia\",\n \"region_code\": \"1234\",\n \"metro_code\": 1234,\n \"zip_code\": \"1234\",\n \"city\": \"Bornyasherk\",\n \"latitude\": 63.416667,\n \"longitude\": 10.416667,\n \"time_zone\": \"Europe/Bornyasherk\",\n \"asn\": \"AS59795\",\n \"asn_org\": \"Hosting4Real\",\n \"hostname\": \"localhost\",\n \"user_agent\": {\n \"product\": \"curl\",\n \"version\": \"7.2.6.0\",\n \"raw_value\": \"curl/7.2.6.0\"\n }\n}", 200},
|
||||
{s.URL + "/port/foo", "{\n \"error\": \"invalid port: foo\"\n}", 400},
|
||||
{s.URL + "/port/0", "{\n \"error\": \"invalid port: 0\"\n}", 400},
|
||||
{s.URL + "/port/65537", "{\n \"error\": \"invalid port: 65537\"\n}", 400},
|
||||
{s.URL + "/port/foo", "{\n \"status\": 400,\n \"error\": \"invalid port: foo\"\n}", 400},
|
||||
{s.URL + "/port/0", "{\n \"status\": 400,\n \"error\": \"invalid port: 0\"\n}", 400},
|
||||
{s.URL + "/port/65537", "{\n \"status\": 400,\n \"error\": \"invalid port: 65537\"\n}", 400},
|
||||
{s.URL + "/port/31337", "{\n \"ip\": \"127.0.0.1\",\n \"port\": 31337,\n \"reachable\": true\n}", 200},
|
||||
{s.URL + "/port/80", "{\n \"ip\": \"127.0.0.1\",\n \"port\": 80,\n \"reachable\": true\n}", 200}, // checking that our test server is reachable on port 80
|
||||
{s.URL + "/port/80?ip=1.3.3.7", "{\n \"ip\": \"127.0.0.1\",\n \"port\": 80,\n \"reachable\": true\n}", 200}, // ensuring that the "ip" parameter is not usable to check remote host ports
|
||||
{s.URL + "/?ip=1.3.3.7/country", "{\n \"error\": \"could not parse IP: 1.3.3.7/country\"\n}", 400},
|
||||
{s.URL + "/foo", "{\n \"error\": \"404 page not found\"\n}", 404},
|
||||
{s.URL + "/foo", "{\n \"status\": 404,\n \"error\": \"404 page not found\"\n}", 404},
|
||||
{s.URL + "/health", `{"status":"OK"}`, 200},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue