-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqurl_basic_selectors_class_test.go
66 lines (62 loc) · 2.04 KB
/
qurl_basic_selectors_class_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
// Copyright 2017 The qurl Authors. All rights reserved.
package qurl
import (
"fmt"
"net/http"
"testing"
)
func TestClassSelectorNotPresent(t *testing.T) {
targetURL := "https://www.example.com"
requestURL := fmt.Sprintf("/q?url=%s&selector=.content", targetURL)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: `<p class="foo">Page content</title>`,
ExpectedStatusCode: http.StatusOK,
}
response, err := q.Query(freq, req.URL.Query())
if err != nil {
t.Fatal(err)
}
if response.Status != http.StatusOK {
t.Errorf("response status expected to be %d but got %d", http.StatusOK, response.Status)
}
if response.URL != targetURL {
t.Errorf("response url expected to be %s but got %s", targetURL, response.URL)
}
if len(response.Selectors[".content"]) != 0 {
t.Fatalf("response selector '.content' expected to have zero elements but got '%v'", len(response.Selectors[".content"]))
}
}
func TestClassSelectorPresent(t *testing.T) {
targetURL := "https://www.example.com"
requestURL := fmt.Sprintf("/q?url=%s&selector=.content", targetURL)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: `<p class="content">Page content</title>`,
ExpectedStatusCode: http.StatusOK,
}
response, err := q.Query(freq, req.URL.Query())
if err != nil {
t.Fatal(err)
}
if response.Status != http.StatusOK {
t.Errorf("response status expected to be %d but got %d", http.StatusOK, response.Status)
}
if response.URL != targetURL {
t.Errorf("response url expected to be %s but got %s", targetURL, response.URL)
}
if len(response.Selectors[".content"]) != 1 {
t.Fatalf("response selector '.content' expected to have one element but got '%v'", len(response.Selectors[".content"]))
}
if response.Selectors[".content"][0].Text != "Page content" {
t.Fatalf("response selector '.content' expected to be %s but got %s", "Page content", response.Selectors[".content"][0].Text)
}
}