58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package playtogo
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func ApiClient_TokenTestAuth_Success(t *testing.T) {
|
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
token := r.Header.Get("Authorization")
|
|
|
|
valData := strings.Split(token, " ")
|
|
|
|
if len(valData) == 2 ||
|
|
strings.Contains(strings.ToLower(valData[0]), "authorization") ||
|
|
len(valData[1]) > 5 {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer testServer.Close()
|
|
|
|
client := &Client{
|
|
baseURL: testServer.URL,
|
|
token: rand.Text(),
|
|
httpClient: testServer.Client(),
|
|
}
|
|
|
|
_, err := client.GetDomains(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("ошибка не ожидалсась: %v", err)
|
|
}
|
|
}
|
|
|
|
func ApiClient_TestGetDomains_Success(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}))
|
|
defer ts.Close()
|
|
|
|
client := &Client{
|
|
baseURL: ts.URL,
|
|
token: rand.Text(),
|
|
httpClient: ts.Client(),
|
|
}
|
|
|
|
_, err := client.GetDomains(context.Background())
|
|
if err != nil {
|
|
t.Fatal("ошибка не ожидалась")
|
|
}
|
|
|
|
}
|