mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-10 07:27:22 +01:00
parent
255826db99
commit
ccd3c7a7bb
13
api/api.go
13
api/api.go
@ -156,6 +156,17 @@ func (a *API) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) JSONAllHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
|
contentType := APPLICATION_JSON
|
||||||
|
b, err := json.MarshalIndent(r.Header, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return internalServerError(err).WithContentType(contentType)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
w.Write(b)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
_, v, err := headerPairFromRequest(r)
|
_, v, err := headerPairFromRequest(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -230,6 +241,8 @@ func (a *API) Handlers() http.Handler {
|
|||||||
|
|
||||||
// JSON
|
// JSON
|
||||||
r.Handle("/", appHandler(a.JSONHandler)).Methods("GET").Headers("Accept", APPLICATION_JSON)
|
r.Handle("/", appHandler(a.JSONHandler)).Methods("GET").Headers("Accept", APPLICATION_JSON)
|
||||||
|
r.Handle("/all", appHandler(a.JSONAllHandler)).Methods("GET").Headers("Accept", APPLICATION_JSON)
|
||||||
|
r.Handle("/all.json", appHandler(a.JSONAllHandler)).Methods("GET")
|
||||||
r.Handle("/{header}", appHandler(a.JSONHandler)).Methods("GET").Headers("Accept", APPLICATION_JSON)
|
r.Handle("/{header}", appHandler(a.JSONHandler)).Methods("GET").Headers("Accept", APPLICATION_JSON)
|
||||||
r.Handle("/{header}.json", appHandler(a.JSONHandler)).Methods("GET")
|
r.Handle("/{header}.json", appHandler(a.JSONHandler)).Methods("GET")
|
||||||
|
|
||||||
|
@ -39,6 +39,10 @@ func TestGetIP(t *testing.T) {
|
|||||||
return fmt.Sprintf("{\n \"%s\": \"%s\"\n}", k, v)
|
return fmt.Sprintf("{\n \"%s\": \"%s\"\n}", k, v)
|
||||||
}
|
}
|
||||||
s := httptest.NewServer(New().Handlers())
|
s := httptest.NewServer(New().Handlers())
|
||||||
|
jsonAll := "{\n \"Accept-Encoding\": [\n \"gzip\"\n ]," +
|
||||||
|
"\n \"X-Ifconfig-Country\": [\n \"\"\n ]," +
|
||||||
|
"\n \"X-Ifconfig-Ip\": [\n \"127.0.0.1\"\n ]\n}"
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
url string
|
url string
|
||||||
json bool
|
json bool
|
||||||
@ -53,7 +57,9 @@ func TestGetIP(t *testing.T) {
|
|||||||
{s.URL, true, toJSON("x-ifconfig-ip", "127.0.0.1"), "", 200},
|
{s.URL, true, toJSON("x-ifconfig-ip", "127.0.0.1"), "", 200},
|
||||||
{s.URL + "/foo", false, "no value found for: foo", "curl/7.26.0", 404},
|
{s.URL + "/foo", false, "no value found for: foo", "curl/7.26.0", 404},
|
||||||
{s.URL + "/foo", true, "{\n \"error\": \"no value found for: foo\"\n}", "curl/7.26.0", 404},
|
{s.URL + "/foo", true, "{\n \"error\": \"no value found for: foo\"\n}", "curl/7.26.0", 404},
|
||||||
|
{s.URL + "/all.json", false, jsonAll, "", 200},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
out, status, err := httpGet(tt.url, tt.json, tt.userAgent)
|
out, status, err := httpGet(tt.url, tt.json, tt.userAgent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,6 +61,8 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
<td><code><span class="command">{{ .Cmd.String }}</span> ifconfig.co/all.json</code></td>
|
||||||
|
<td><pre class="response">{{ .JSON }}</pre></td>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user