File: link_test.go

package info (click to toggle)
golang-github-tent-http-link-go 0.0~git20130702.0.ac974c6-2~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 76 kB
  • sloc: makefile: 3
file content (67 lines) | stat: -rw-r--r-- 1,844 bytes parent folder | download | duplicates (3)
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
package link

import (
	"testing"

	. "gopkg.in/check.v1"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type LinkSuite struct{}

var _ = Suite(&LinkSuite{})

// TODO: add more tests
var linkParseTests = []struct {
	in  string
	out []Link
}{
	{
		"<http://example.com/TheBook/chapter2>; rel=\"previous\";\n    title=\"previous chapter\"",
		[]Link{{URI: "http://example.com/TheBook/chapter2", Rel: "previous", Params: map[string]string{"title": "previous chapter"}}},
	},
	{
		"</TheBook/chapter2>;\n rel=\"previous\"; title*=UTF-8'de'letztes%20Kapitel,\n </TheBook/chapter4>;\n rel=\"next\"; title*=UTF-8'de'n%c3%a4chstes%20Kapitel",
		[]Link{
			{URI: "/TheBook/chapter2", Rel: "previous", Params: map[string]string{"title*": "UTF-8'de'letztes%20Kapitel"}},
			{URI: "/TheBook/chapter4", Rel: "next", Params: map[string]string{"title*": "UTF-8'de'n%c3%a4chstes%20Kapitel"}},
		},
	},
}

func (s *LinkSuite) TestLinkParsing(c *C) {
	for i, t := range linkParseTests {
		res, err := Parse(t.in)
		c.Assert(err, IsNil, Commentf("test %d", i))
		c.Assert(res, DeepEquals, t.out, Commentf("test %d", i))
	}
}

var linkFormatTests = []struct {
	in  []Link
	out string
}{
	{
		[]Link{{URI: "/a", Rel: "foo", Params: map[string]string{"a": "b", "c": "d"}}},
		`</a>; rel="foo"; a="b"; c="d"`,
	},
	{
		[]Link{
			{URI: "/b", Rel: "foo", Params: map[string]string{"a": "b", "c": "d"}},
			{URI: "/a", Rel: "foo", Params: map[string]string{"a": "b", "c": "d"}}},
		`</b>; rel="foo"; a="b"; c="d", </a>; rel="foo"; a="b"; c="d"`,
	},
}

func (s *LinkSuite) TestLinkGeneration(c *C) {
	for i, t := range linkFormatTests {
		res := Format(t.in)
		cm := Commentf("test %d", i)
		c.Assert(res, Equals, t.out, cm)
		parsed, err := Parse(res)
		c.Assert(err, IsNil, cm)
		c.Assert(parsed, DeepEquals, t.in, cm)
	}
}