Skip to content

Commit 5716e59

Browse files
committed
Re-add test
1 parent 7f9a0f6 commit 5716e59

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

deviceauth_test.go

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,36 +104,51 @@ func ExampleConfig_DeviceAuth() {
104104
fmt.Println(token)
105105
}
106106

107-
func TestDeviceAuthTokenRetrieveErrorJSON(t *testing.T) {
108-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
109-
if r.URL.String() != "/device" {
110-
t.Errorf("Unexpected device auth request URL, %v is found.", r.URL)
107+
func TestDeviceAuthTokenRetrieveError(t *testing.T) {
108+
runner := func(responseFun func(w http.ResponseWriter)) func(t *testing.T) {
109+
return func(t *testing.T) {
110+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111+
if r.URL.String() != "/device" {
112+
t.Errorf("Unexpected device auth request URL, %v is found.", r.URL)
113+
}
114+
responseFun(w)
115+
}))
116+
defer ts.Close()
117+
conf := newConf(ts.URL)
118+
_, err := conf.DeviceAuth(context.Background())
119+
if err == nil {
120+
t.Fatalf("got no error, expected one")
121+
}
122+
re, ok := err.(*RetrieveError)
123+
if !ok {
124+
t.Fatalf("got %T error, expected *RetrieveError; error was: %v", err, err)
125+
}
126+
expected := `oauth2: "invalid_grant" "sometext"`
127+
if errStr := err.Error(); errStr != expected {
128+
t.Fatalf("got %#v, expected %#v", errStr, expected)
129+
}
130+
expected = "invalid_grant"
131+
if re.ErrorCode != expected {
132+
t.Fatalf("got %#v, expected %#v", re.ErrorCode, expected)
133+
}
134+
expected = "sometext"
135+
if re.ErrorDescription != expected {
136+
t.Fatalf("got %#v, expected %#v", re.ErrorDescription, expected)
137+
}
111138
}
139+
}
140+
141+
t.Run("TestDeviceAuthTokenRetrieveErrorUrlEncoding", runner(func(w http.ResponseWriter) {
142+
w.Header().Set("Content-type", "application/x-www-form-urlencoded")
143+
// "The authorization server responds with an HTTP 400 (Bad Request)" https://www.rfc-editor.org/rfc/rfc6749#section-5.2
144+
w.WriteHeader(http.StatusBadRequest)
145+
w.Write([]byte(`error=invalid_grant&error_description=sometext`))
146+
}))
147+
148+
t.Run("TestDeviceAuthTokenRetrieveErrorJSON", runner(func(w http.ResponseWriter) {
112149
w.Header().Set("Content-type", "application/json")
113150
// "The authorization server responds with an HTTP 400 (Bad Request)" https://www.rfc-editor.org/rfc/rfc6749#section-5.2
114151
w.WriteHeader(http.StatusBadRequest)
115152
w.Write([]byte(`{"error": "invalid_grant", "error_description": "sometext"}`))
116153
}))
117-
defer ts.Close()
118-
conf := newConf(ts.URL)
119-
_, err := conf.DeviceAuth(context.Background())
120-
if err == nil {
121-
t.Fatalf("got no error, expected one")
122-
}
123-
re, ok := err.(*RetrieveError)
124-
if !ok {
125-
t.Fatalf("got %T error, expected *RetrieveError; error was: %v", err, err)
126-
}
127-
expected := `oauth2: "invalid_grant" "sometext"`
128-
if errStr := err.Error(); errStr != expected {
129-
t.Fatalf("got %#v, expected %#v", errStr, expected)
130-
}
131-
expected = "invalid_grant"
132-
if re.ErrorCode != expected {
133-
t.Fatalf("got %#v, expected %#v", re.ErrorCode, expected)
134-
}
135-
expected = "sometext"
136-
if re.ErrorDescription != expected {
137-
t.Fatalf("got %#v, expected %#v", re.ErrorDescription, expected)
138-
}
139154
}

0 commit comments

Comments
 (0)