Add SetToken function to NewAPIv4Client (#11722)
Этот коммит содержится в:
коммит произвёл
Ben Schumacher
родитель
bd270aecbd
Коммит
8cdbc734b9
@@ -6,6 +6,7 @@ package model
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -56,3 +57,47 @@ func TestClient4CreatePost(t *testing.T) {
|
||||
_, resp := client.CreatePost(post)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestClient4SetToken(t *testing.T) {
|
||||
expected := NewId()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authHeader := r.Header.Get(HEADER_AUTH)
|
||||
|
||||
token := strings.Split(authHeader, HEADER_BEARER)
|
||||
|
||||
if len(token) < 2 {
|
||||
t.Errorf("wrong authorization header format, got %s, expected: %s %s", authHeader, HEADER_BEARER, expected)
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, strings.TrimSpace(token[1]))
|
||||
}))
|
||||
|
||||
client := NewAPIv4Client(server.URL)
|
||||
client.SetToken(expected)
|
||||
|
||||
_, resp := client.GetMe("")
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestClient4MockSession(t *testing.T) {
|
||||
expected := NewId()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authHeader := r.Header.Get(HEADER_AUTH)
|
||||
|
||||
token := strings.Split(authHeader, HEADER_BEARER)
|
||||
|
||||
if len(token) < 2 {
|
||||
t.Errorf("wrong authorization header format, got %s, expected: %s %s", authHeader, HEADER_BEARER, expected)
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, strings.TrimSpace(token[1]))
|
||||
}))
|
||||
|
||||
client := NewAPIv4Client(server.URL)
|
||||
client.MockSession(expected)
|
||||
|
||||
_, resp := client.GetMe("")
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user