-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhttp_client_bearer_test.go
54 lines (45 loc) · 1.43 KB
/
http_client_bearer_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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
// TestHttpClientWithBearerTransport tests the addition of the Authorization header.
func TestHttpClientWithBearerTransport(t *testing.T) {
// Define the expected Bearer token
token := "test_bearer_token"
// Set up a test HTTP server
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Retrieve the Authorization header from the request
authHeader := r.Header.Get("Authorization")
expectedHeader := fmt.Sprintf("Bearer %s", token)
// Check if the Authorization header matches the expected value
if authHeader != expectedHeader {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Return a success response
w.WriteHeader(http.StatusOK)
io.WriteString(w, "Success")
}))
defer testServer.Close()
// Create an HTTP client with the custom transport
client := NewHttpClientWithBearerTransport(token)
// Create a new HTTP request to the test server
req, err := http.NewRequest("GET", testServer.URL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
// Perform the request using the custom client
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %v", err)
}
defer resp.Body.Close()
// Check if the status code is 200 OK
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code 200 OK, got %d", resp.StatusCode)
}
}