-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
73 lines (63 loc) · 1.73 KB
/
client_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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestSetPassword(t *testing.T) {
// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/set_password" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
// Simulate a successful response
w.WriteHeader(http.StatusOK)
response := Response{Status: http.StatusOK, Link: "example.com", Ttl: 3600}
json.NewEncoder(w).Encode(response)
}))
defer ts.Close()
// Create a client with the test server's URL
client := NewClient(ts.URL)
// Test a successful request
response, err := client.SetPassword("test_password")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !response.IsSuccessful() {
t.Error("expected successful response")
}
// Test a failed request
ts.Close()
response, err = client.SetPassword("test_password")
if err == nil {
t.Error("expected an error, got nil")
}
if response != nil {
t.Errorf("expected nil response, got: %v", response)
}
}
func TestSetPassword_HttpError(t *testing.T) {
client := NewClient("http://nonexistent-url.com")
// Test an error during HTTP request
_, err := client.SetPassword("test_password")
if err == nil {
t.Error("expected an error, got nil")
}
}
func TestResponse_IsSuccessful(t *testing.T) {
tests := []struct {
response *Response
expected bool
}{
{&Response{Status: http.StatusOK}, true},
{&Response{Status: http.StatusNotFound}, false},
{&Response{Status: http.StatusInternalServerError}, false},
}
for _, test := range tests {
if result := test.response.IsSuccessful(); result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
}
}