File: request_test.go

package info (click to toggle)
golang-github-sethvargo-go-fastly 1.2.1%2Bgit20190805.5c6c8bd-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,584 kB
  • sloc: makefile: 71
file content (55 lines) | stat: -rw-r--r-- 1,484 bytes parent folder | download | duplicates (2)
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
package fastly

import (
	"net/url"
	"strings"
	"testing"
)

func TestClient_RawRequest(t *testing.T) {
	validAPIHosts := []string{
		"https://api.fastly.com",
		"https://api.fastly.com/",
	}
	purgeAPIPaths := []string{
		"/service/myservice/purge/",
		"service/myservice/purge/",
	}
	cacheKeys := []string{
		"/",
		"text//text",
		"$-_.+!*'(),,;/?:@=&\"<>#%{}|\\^~[]`",
	}
	c := &Client{}
	for _, h := range validAPIHosts {
		var err error
		c.url, err = url.Parse(h)
		if err != nil {
			t.Fatalf("Unable to parse url %s: %s\n", h, err)
		}
		for _, p := range purgeAPIPaths {
			for _, k := range cacheKeys {
				r, err := c.RawRequest("GET", p+url.PathEscape(k), nil)
				// Cannot test results for success if we get an error
				if err != nil {
					t.Fatal("Could not make RawRequest for ", h, p, k)
				}
				t.Log("Encoded path returned: ", r.URL.EscapedPath())
				pk := p + url.PathEscape(k)
				if p[0] != '/' {
					pk = "/" + pk
				}
				t.Log("Encoded path expected: ", pk)
				// Insure we don't get a path starting with an extra slash
				// e.g. //service/myservice/purge/mykey
				if r.URL.Path[1] == '/' {
					t.Fatalf("Host and APIPath were joined incorrectly. Got: %s\n", r.URL.Path)
				}
				// Insure the encoded path isn't altered
				if strings.Index(r.URL.EscapedPath(), p+url.PathEscape(k)) == -1 {
					t.Fatalf("RawRequest altered the encoded path. New encoded path: %s, expecting: %s\n", r.URL.EscapedPath(), p+url.PathEscape(k))
				}
			}
		}
	}
}