|
| 1 | +// Copyright 2014 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package internal |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "net/url" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func TestDeviceAuth_ClientAuthnInParams(t *testing.T) { |
| 17 | + styleCache := new(AuthStyleCache) |
| 18 | + const clientID = "client-id" |
| 19 | + const clientSecret = "client-secret" |
| 20 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | + if got, want := r.FormValue("client_id"), clientID; got != want { |
| 22 | + t.Errorf("client_id = %q; want %q", got, want) |
| 23 | + } |
| 24 | + if got, want := r.FormValue("client_secret"), clientSecret; got != want { |
| 25 | + t.Errorf("client_secret = %q; want %q", got, want) |
| 26 | + } |
| 27 | + io.WriteString(w, `{"device_code":"code","user_code":"user_code","verification_uri":"http://example.device.com","expires_in":300,"interval":5}`) |
| 28 | + })) |
| 29 | + defer ts.Close() |
| 30 | + _, err := RetrieveDeviceAuth(context.Background(), clientID, clientSecret, ts.URL, url.Values{}, AuthStyleInParams, styleCache) |
| 31 | + if err != nil { |
| 32 | + t.Errorf("RetrieveDeviceAuth = %v; want no error", err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestDeviceAuth_ClientAuthnInHeader(t *testing.T) { |
| 37 | + styleCache := new(AuthStyleCache) |
| 38 | + const clientID = "client-id" |
| 39 | + const clientSecret = "client-secret" |
| 40 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | + u, p, ok := r.BasicAuth() |
| 42 | + if !ok { |
| 43 | + io.WriteString(w, `{"error":"invalid_client"}`) |
| 44 | + w.WriteHeader(http.StatusBadRequest) |
| 45 | + } |
| 46 | + if got, want := u, clientID; got != want { |
| 47 | + io.WriteString(w, `{"error":"invalid_client"}`) |
| 48 | + w.WriteHeader(http.StatusBadRequest) |
| 49 | + } |
| 50 | + if got, want := p, clientSecret; got != want { |
| 51 | + io.WriteString(w, `{"error":"invalid_client"}`) |
| 52 | + w.WriteHeader(http.StatusBadRequest) |
| 53 | + } |
| 54 | + io.WriteString(w, `{"device_code":"code","user_code":"user_code","verification_uri":"http://example.device.com","expires_in":300,"interval":5}`) |
| 55 | + })) |
| 56 | + defer ts.Close() |
| 57 | + _, err := RetrieveDeviceAuth(context.Background(), clientID, clientSecret, ts.URL, url.Values{}, AuthStyleInHeader, styleCache) |
| 58 | + if err != nil { |
| 59 | + t.Errorf("RetrieveDeviceAuth = %v; want no error", err) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestDeviceAuth_ClientAuthnProbe(t *testing.T) { |
| 64 | + styleCache := new(AuthStyleCache) |
| 65 | + const clientID = "client-id" |
| 66 | + const clientSecret = "client-secret" |
| 67 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 68 | + u, p, ok := r.BasicAuth() |
| 69 | + if !ok { |
| 70 | + io.WriteString(w, `{"error":"invalid_client"}`) |
| 71 | + w.WriteHeader(http.StatusBadRequest) |
| 72 | + } |
| 73 | + if got, want := u, clientID; got != want { |
| 74 | + io.WriteString(w, `{"error":"invalid_client"}`) |
| 75 | + w.WriteHeader(http.StatusBadRequest) |
| 76 | + } |
| 77 | + if got, want := p, clientSecret; got != want { |
| 78 | + io.WriteString(w, `{"error":"invalid_client"}`) |
| 79 | + w.WriteHeader(http.StatusBadRequest) |
| 80 | + } |
| 81 | + io.WriteString(w, `{"device_code":"code","user_code":"user_code","verification_uri":"http://example.device.com","expires_in":300,"interval":5}`) |
| 82 | + })) |
| 83 | + defer ts.Close() |
| 84 | + _, err := RetrieveDeviceAuth(context.Background(), clientID, clientSecret, ts.URL, url.Values{}, AuthStyleUnknown, styleCache) |
| 85 | + if err != nil { |
| 86 | + t.Errorf("RetrieveDeviceAuth = %v; want no error", err) |
| 87 | + } |
| 88 | +} |
0 commit comments