-
Notifications
You must be signed in to change notification settings - Fork 12
/
http_test.go
123 lines (113 loc) · 2.95 KB
/
http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package oauth2client
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync/atomic"
"testing"
"github.com/luraproject/lura/v2/config"
)
func TestClient(t *testing.T) {
clientID := "some_client_id"
clientSecret := "some_client_secret"
scopes := "scope1,scope2"
audience := "http://api.example.com"
token := "03807cb390319329bdf6c777d4dfae9c0d3b3c35"
okidoki := "Hello, client"
expectedValues := url.Values{
"audience": {audience},
"grant_type": {"client_credentials"},
}
var tokenIssued atomic.Value
tokenIssued.Store(false)
expectedBody := fmt.Sprintf("%s&scope=%s", expectedValues.Encode(), strings.ReplaceAll(scopes, ",", "+"))
tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tokenIssued.Load().(bool) {
t.Error("token issuer was asked for more than a single token")
return
}
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Error("unexpected content type:", r.Header.Get("Content-Type"))
return
}
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
log.Println(err)
return
}
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
t.Error("Not authorized", s)
return
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
t.Error(err.Error())
return
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
t.Error("Not authorized", pair)
return
}
if pair[0] != clientID || pair[1] != clientSecret {
t.Error("Not authorized", pair)
return
}
if string(body) != expectedBody {
t.Error("unexpected body! have:", string(body), "want:", expectedBody)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"access_token":"%s","expires_in":3600,"token_type":"bearer"}`, token)
tokenIssued.Store(true)
}))
defer tokenServer.Close()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != fmt.Sprintf("Bearer %s", token) {
t.Error("unexpected token:", r.Header.Get("Authorization"))
return
}
fmt.Fprint(w, okidoki)
}))
defer ts.Close()
c := NewHTTPClient(&config.Backend{
ExtraConfig: map[string]interface{}{
Namespace: map[string]interface{}{
"client_id": clientID,
"client_secret": clientSecret,
"token_url": tokenServer.URL,
"scopes": scopes,
"endpoint_params": map[string]interface{}{
"audience": []interface{}{audience},
},
},
},
})
client := c(context.Background())
for i := 0; i < 5; i++ {
resp, err := client.Get(ts.URL)
if err != nil {
log.Println(err)
t.Error(err)
return
}
response, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println(err)
t.Error(err)
return
}
if string(response) != okidoki {
t.Error("unexpected body:", string(response))
}
}
}