This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequests_test.go
160 lines (142 loc) · 4.73 KB
/
requests_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Hockeypuck - OpenPGP key server
Copyright (C) 2012-2014 Casey Marshall
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hkp
import (
"bytes"
"net/http"
"net/url"
gc "gopkg.in/check.v1"
)
/*
These server tests primarily exercise the request parsing and routing
of requests and responses.
*/
type RequestsSuite struct{}
var _ = gc.Suite(&RequestsSuite{})
func (s *RequestsSuite) TestGetKeyword(c *gc.C) {
// basic search
testUrl, err := url.Parse("/pks/lookup?op=get&search=alice")
c.Assert(err, gc.IsNil)
req := &http.Request{
Method: "GET",
URL: testUrl}
lookup, err := ParseLookup(req)
c.Assert(err, gc.IsNil)
c.Assert(OperationGet, gc.Equals, lookup.Op)
c.Assert("alice", gc.Equals, lookup.Search)
c.Assert(lookup.Options, gc.HasLen, 0)
c.Assert(lookup.Fingerprint, gc.Equals, false)
c.Assert(lookup.Exact, gc.Equals, false)
}
func (s *RequestsSuite) TestGetFp(c *gc.C) {
// fp search
testUrl, err := url.Parse("/pks/lookup?op=get&search=0xdecafbad&options=mr,nm&fingerprint=on&exact=on")
c.Assert(err, gc.IsNil)
req := &http.Request{
Method: "GET",
URL: testUrl}
lookup, err := ParseLookup(req)
c.Assert(err, gc.IsNil)
c.Assert(OperationGet, gc.Equals, lookup.Op)
c.Assert("0xdecafbad", gc.Equals, lookup.Search)
c.Assert(lookup.Options[OptionMachineReadable], gc.Equals, true)
c.Assert(lookup.Options[OptionNotModifiable], gc.Equals, true)
c.Assert(lookup.Fingerprint, gc.Equals, true)
c.Assert(lookup.Exact, gc.Equals, true)
}
func (s *RequestsSuite) TestIndex(c *gc.C) {
// op=index
testUrl, err := url.Parse("/pks/lookup?op=index&search=sharin") // as in, foo
c.Assert(err, gc.IsNil)
req := &http.Request{
Method: "GET",
URL: testUrl}
lookup, err := ParseLookup(req)
c.Assert(err, gc.IsNil)
c.Assert(lookup.Op, gc.Equals, OperationIndex)
}
func (s *RequestsSuite) TestVindex(c *gc.C) {
// op=vindex
testUrl, err := url.Parse("/pks/lookup?op=vindex&search=bob") // as in, foo
c.Assert(err, gc.IsNil)
req := &http.Request{
Method: "GET",
URL: testUrl}
lookup, err := ParseLookup(req)
c.Assert(err, gc.IsNil)
c.Assert(OperationVIndex, gc.Equals, lookup.Op)
}
func (s *RequestsSuite) TestMissingSearch(c *gc.C) {
// create an op=get lookup without the required search term
testUrl, err := url.Parse("/pks/lookup?op=get")
c.Assert(err, gc.IsNil)
req := &http.Request{
Method: "GET",
URL: testUrl}
_, err = ParseLookup(req)
// error without search term
c.Assert(err, gc.NotNil)
}
func (s *RequestsSuite) TestNoSuchOp(c *gc.C) {
// hockeypuck does not know how to do a barrel roll
testUrl, err := url.Parse("/pks/lookup?op=barrelroll")
c.Assert(err, gc.IsNil)
req := &http.Request{
Method: "GET",
URL: testUrl}
_, err = ParseLookup(req)
// Unknown operation
c.Assert(err, gc.NotNil)
}
func (s *RequestsSuite) TestAdd(c *gc.C) {
// adding a key
testUrl, err := url.Parse("/pks/add")
c.Assert(err, gc.IsNil)
postData := make(map[string][]string)
postData["keytext"] = []string{"sus llaves aqui"}
req, err := http.NewRequest("POST", testUrl.String(), bytes.NewBuffer(nil))
req.PostForm = url.Values(postData)
add, err := ParseAdd(req)
c.Assert(err, gc.IsNil)
c.Assert("sus llaves aqui", gc.Equals, add.Keytext)
c.Assert(add.Options, gc.HasLen, 0)
}
func (s *RequestsSuite) TestAddOptions(c *gc.C) {
// adding a key with options
testUrl, err := url.Parse("/pks/add?options=mr")
c.Assert(err, gc.IsNil)
postData := make(map[string][]string)
postData["keytext"] = []string{"sus llaves estan aqui"}
postData["options"] = []string{"mr"}
req, err := http.NewRequest("POST", testUrl.String(), bytes.NewBuffer(nil))
req.PostForm = url.Values(postData)
add, err := ParseAdd(req)
c.Assert(err, gc.IsNil)
c.Assert("sus llaves estan aqui", gc.Equals, add.Keytext)
c.Assert(add.Options[OptionMachineReadable], gc.Equals, true)
c.Assert(add.Options[OptionNotModifiable], gc.Equals, false)
}
func (s *RequestsSuite) TestAddMissingKey(c *gc.C) {
// here's my key. wait, i forgot it.
testUrl, err := url.Parse("/pks/add")
c.Assert(err, gc.IsNil)
postData := make(map[string][]string)
req := &http.Request{
Method: "POST",
URL: testUrl,
Form: url.Values(postData)}
_, err = ParseAdd(req)
// error without keytext
c.Assert(err, gc.NotNil)
}