-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqurl_test.go
80 lines (76 loc) · 2.12 KB
/
qurl_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
74
75
76
77
78
79
80
// Copyright 2017 The qurl Authors. All rights reserved.
package qurl
import (
"fmt"
"net/http"
"testing"
)
func TestQuery(t *testing.T) {
targetURL := "https://www.example.com"
requestURL := fmt.Sprintf("/q?url=%s", targetURL)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: "Hello world!",
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)
}
}
func TestQueryInvalidURL(t *testing.T) {
targetURL := "invalidurl"
requestURL := fmt.Sprintf("/q?url=%s", targetURL)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: "",
ExpectedStatusCode: http.StatusInternalServerError,
}
response, err := q.Query(freq, req.URL.Query())
if err != nil {
t.Fatal(err)
}
if response.Status != http.StatusInternalServerError {
t.Errorf("response status expected to be %d but got %d", http.StatusInternalServerError, response.Status)
}
if response.URL != targetURL {
t.Errorf("response url expected to be %s but got %s", targetURL, response.URL)
}
}
func TestFailFetchURL(t *testing.T) {
targetURL := "http://localhost"
requestURL := fmt.Sprintf("/q?url=%s", targetURL)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
t.Fatal(err)
}
q := &QURL{}
freq := &FakeRequest{
ExpectedBody: "",
ExpectedStatusCode: http.StatusInternalServerError,
}
response, err := q.Query(freq, req.URL.Query())
if err != nil {
t.Fatal(err)
}
if response.Status != http.StatusInternalServerError {
t.Errorf("response status expected to be %d but got %d", http.StatusInternalServerError, response.Status)
}
if response.URL != targetURL {
t.Errorf("response url expected to be %s but got %s", targetURL, response.URL)
}
}