File: netutils_test.go

package info (click to toggle)
golang-github-vulcand-oxy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 728 kB
  • sloc: makefile: 14
file content (101 lines) | stat: -rw-r--r-- 2,438 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package utils

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
)

// Make sure copy does it right, so the copied url is safe to alter without modifying the other.
func TestCopyUrl(t *testing.T) {
	userinfo := url.UserPassword("foo", "secret")

	urlA := &url.URL{
		Scheme:   "http",
		Host:     "localhost:5000",
		Path:     "/upstream",
		Opaque:   "opaque",
		RawQuery: "a=1&b=2",
		Fragment: "#hello",
		User:     userinfo,
	}

	urlB := CopyURL(urlA)
	assert.Equal(t, urlA, urlB)

	*userinfo = *url.User("bar")

	assert.Equal(t, urlA.User, userinfo)
	assert.NotEqual(t, urlA.User, urlB.User)

	urlB.Scheme = "https"

	assert.NotEqual(t, urlA, urlB)
}

// Make sure copy headers is not shallow and copies all headers.
func TestCopyHeaders(t *testing.T) {
	source, destination := make(http.Header), make(http.Header)
	source.Add("a", "b")
	source.Add("c", "d")

	CopyHeaders(destination, source)

	assert.Equal(t, "b", destination.Get("a"))
	assert.Equal(t, "d", destination.Get("c"))

	// make sure that altering source does not affect the destination
	source.Del("a")

	assert.Equal(t, "", source.Get("a"))
	assert.Equal(t, "b", destination.Get("a"))
}

func TestHasHeaders(t *testing.T) {
	source := make(http.Header)
	source.Add("a", "b")
	source.Add("c", "d")

	assert.True(t, HasHeaders([]string{"a", "f"}, source))
	assert.False(t, HasHeaders([]string{"i", "j"}, source))
}

func TestRemoveHeaders(t *testing.T) {
	source := make(http.Header)
	source.Add("a", "b")
	source.Add("a", "m")
	source.Add("c", "d")

	RemoveHeaders(source, "a")

	assert.Equal(t, "", source.Get("a"))
	assert.Equal(t, "d", source.Get("c"))
}

func BenchmarkCopyHeaders(b *testing.B) {
	dstHeaders := make([]http.Header, 0, b.N)
	sourceHeaders := make([]http.Header, 0, b.N)
	for n := 0; n < b.N; n++ {
		// example from a reverse proxy merging headers
		d := http.Header{}
		d.Add("Request-Id", "1bd36bcc-a0d1-4fc7-aedc-20bbdefa27c5")
		dstHeaders = append(dstHeaders, d)

		s := http.Header{}
		s.Add("Content-Length", "374")
		s.Add("Context-Type", "text/html; charset=utf-8")
		s.Add("Etag", `"op14g6ae"`)
		s.Add("Last-Modified", "Wed, 26 Apr 2017 18:24:06 GMT")
		s.Add("Server", "Caddy")
		s.Add("Date", "Fri, 28 Apr 2017 15:54:01 GMT")
		s.Add("Accept-Ranges", "bytes")
		sourceHeaders = append(sourceHeaders, s)
	}
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		CopyHeaders(dstHeaders[n], sourceHeaders[n])
	}
}