-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqurl_basic_selectors_id_test.go
More file actions
65 lines (61 loc) · 1.99 KB
/
Copy pathqurl_basic_selectors_id_test.go
File metadata and controls
65 lines (61 loc) · 1.99 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
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2017 The qurl Authors. All rights reserved.
package qurl
import (
"fmt"
"net/http"
"testing"
)
func TestIDSelectorNotPresent(t *testing.T) {
targetURL := "https://www.example.com"
requestURL := fmt.Sprintf("/q?url=%s&selector=#foo", targetURL)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: `<p id="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["#foo"]) != 0 {
t.Fatalf("response selector '#foo' expected to have zero elements but got '%v'", len(response.Selectors["#foo"]))
}
}
func TestIDSelectorPresent(t *testing.T) {
targetURL := "https://www.example.com"
req, err := http.NewRequest("GET", "/q?url="+targetURL+"&selector=%23content", nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: `<p id="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)
}
}