-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
55 lines (45 loc) · 1.17 KB
/
client_test.go
File metadata and controls
55 lines (45 loc) · 1.17 KB
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 browserhttp_test
import (
"net/http"
"strings"
"testing"
"time"
"github.com/gleicon/browserhttp"
)
func TestGETRequest(t *testing.T) {
client := browserhttp.NewClient(15 * time.Second)
err := client.Init()
if err != nil {
t.Fatalf("Init failed: %v", err)
}
defer client.Close()
req, _ := http.NewRequest("GET", "https://example.com", nil)
resp, err := client.Do(req)
if err != nil {
t.Fatalf("GET request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
}
func TestPOSTRequest(t *testing.T) {
client := browserhttp.NewClient(15 * time.Second)
client.UsePersistentTabs(true)
err := client.Init()
if err != nil {
t.Fatalf("Init failed: %v", err)
}
defer client.Close()
data := "name=test&value=123"
req, _ := http.NewRequest("POST", "https://httpbin.org/post", strings.NewReader(data))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
t.Fatalf("POST request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
}