Add fail test cover to TestRegisterApp

pull/37/head
178inaba 2017-04-21 02:29:22 +09:00
parent bb7a3a2892
commit c1bb3c84bc
1 changed files with 33 additions and 4 deletions

View File

@ -10,24 +10,53 @@ import (
) )
func TestRegisterApp(t *testing.T) { func TestRegisterApp(t *testing.T) {
isNotJSON := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} } else if r.URL.Path != "/api/v1/apps" {
if r.URL.Path != "/api/v1/apps" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
} } else if r.FormValue("redirect_uris") != "urn:ietf:wg:oauth:2.0:oob" {
if r.FormValue("redirect_uris") != "urn:ietf:wg:oauth:2.0:oob" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} else if isNotJSON {
isNotJSON = false
fmt.Fprintln(w, `<html><head><title>Apps</title></head></html>`)
return
} }
fmt.Fprintln(w, `{"client_id": "foo", "client_secret": "bar"}`) fmt.Fprintln(w, `{"client_id": "foo", "client_secret": "bar"}`)
return return
})) }))
defer ts.Close() defer ts.Close()
// Status not ok.
_, err := RegisterApp(context.Background(), &AppConfig{
Server: ts.URL,
RedirectURIs: "/",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Error in url.Parse
_, err = RegisterApp(context.Background(), &AppConfig{
Server: ":",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Error in json.NewDecoder
_, err = RegisterApp(context.Background(), &AppConfig{
Server: ts.URL,
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Success.
app, err := RegisterApp(context.Background(), &AppConfig{ app, err := RegisterApp(context.Background(), &AppConfig{
Server: ts.URL, Server: ts.URL,
Scopes: "read write follow", Scopes: "read write follow",