forked from kuja/hamster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
217 lines (176 loc) · 6.94 KB
/
Copy pathclient.js
File metadata and controls
217 lines (176 loc) · 6.94 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var assert = require('assert')
, url = require('url')
, fs = require('fs')
, http = require('http')
, Client = require(__dirname + '/../lib/client')
suite('hamster.Client')
test('#getPathName() supports <namespace>:<resource> path format', function () {
var client = new Client()
assert.equal(client.getPathName('eve:SkillTree'), '/eve/SkillTree.xml.aspx')
assert.equal(client.getPathName('char:AccountBalance'), '/char/AccountBalance.xml.aspx')
assert.equal(client.getPathName('OhNoez'), '/OhNoez.xml.aspx')
})
test('#getPathName() prepends base path', function () {
var client = new Client()
// with trailing slashes
client.seturl('https://api.eveonline.com/')
assert.equal(client.getPathName('/char/AccountBalance.xml.aspx'), '/char/AccountBalance.xml.aspx')
assert.equal(client.getPathName('char:AccountBalance'), '/char/AccountBalance.xml.aspx')
client.seturl('https://api.eveonline.com/char/')
assert.equal(client.getPathName('/AccountBalance.xml.aspx'), '/char/AccountBalance.xml.aspx')
assert.equal(client.getPathName('AccountBalance'), '/char/AccountBalance.xml.aspx')
// without trailing slashes
client.seturl('https://api.eveonline.com')
assert.equal(client.getPathName('/char/AccountBalance.xml.aspx'), '/char/AccountBalance.xml.aspx')
assert.equal(client.getPathName('char:AccountBalance'), '/char/AccountBalance.xml.aspx')
client.seturl('https://api.eveonline.com/char')
assert.equal(client.getPathName('/AccountBalance.xml.aspx'), '/char/AccountBalance.xml.aspx')
assert.equal(client.getPathName('AccountBalance'), '/char/AccountBalance.xml.aspx')
})
test('#getRequestUrl() returns URL object', function () {
var client = new Client()
, actual
, expected
actual = url.format(client.getRequesturl('char:AccountBalance', {characterID: 1234}))
expected = 'https://api.eveonline.com/char/AccountBalance.xml.aspx?characterID=1234'
assert.equal(actual, expected)
actual = url.format(client.getRequesturl('eve:SkillTree'))
expected = 'https://api.eveonline.com/eve/SkillTree.xml.aspx'
assert.equal(actual, expected)
actual = url.format(client.getRequesturl('foo:bar', {c: 'c', a: 'a', b: 'b'}))
expected = 'https://api.eveonline.com/foo/bar.xml.aspx?c=c&a=a&b=b'
assert.equal(actual, expected)
})
test('#getRequestUrl() merges default params', function () {
var client = new Client()
, actual
, expected
client.setParams({keyID: 'herp', vCode: 'derp'})
actual = url.format(client.getRequesturl('char:AccountBalance', {characterID: '1234'}))
expected = 'https://api.eveonline.com/char/AccountBalance.xml.aspx?characterID=1234&keyID=herp&vCode=derp'
assert.equal(actual, expected)
})
test('#getCacheKey() returns URL string', function () {
var client = new Client()
, actual
, expected
actual = client.getCacheKey(client.getRequesturl('server:ServerStatus'))
expected = 'https://api.eveonline.com/server/ServerStatus.xml.aspx'
assert.equal(actual, expected)
actual = client.getCacheKey(client.getRequesturl('char:AccountBalance', {characterID: '12345'}))
expected = 'https://api.eveonline.com/char/AccountBalance.xml.aspx?characterID=12345'
assert.equal(actual, expected)
})
test('#getCacheKey() sorts query string parameters alphebetically', function () {
var client = new Client()
, actual
, expected
actual = client.getCacheKey(client.getRequesturl('foo:bar', {c: 'c', a: 'a', b: 'b'}))
expected = 'https://api.eveonline.com/foo/bar.xml.aspx?a=a&b=b&c=c'
assert.equal(actual, expected)
actual = client.getCacheKey(client.getRequesturl('foo:bar', {b: 'b', c: 'c', a: 'a'}))
expected = 'https://api.eveonline.com/foo/bar.xml.aspx?a=a&b=b&c=c'
assert.equal(actual, expected)
})
test('#parse() can parse simple API response', function (done) {
var client = new Client()
fs.readFile(__dirname + '/simple.xml', function (err, xml) {
fs.readFile(__dirname + '/simple.json', function (err, json) {
client.parse(xml, function (err, result) {
assert.ifError(err)
assert.deepEqual(result, JSON.parse(json))
done()
})
})
})
})
test('#parse() can parse rowsets', function (done) {
var client = new Client()
fs.readFile(__dirname + '/rowset.xml', function (err, xml) {
fs.readFile(__dirname + '/rowset.json', function (err, json) {
client.parse(xml, function (err, result) {
assert.ifError(err)
assert.deepEqual(result, JSON.parse(json))
done()
})
})
})
})
test('#parse() can parse nested rowsets', function (done) {
var client = new Client()
fs.readFile(__dirname + '/alliance-list.xml', function (err, xml) {
fs.readFile(__dirname + '/alliance-list.json', function (err, json) {
client.parse(xml, function (err, result) {
assert.ifError(err)
assert.deepEqual(result, JSON.parse(json))
done()
})
})
})
})
test('#parse() can parse mutli-keyed rowsets', function (done) {
var client = new Client()
fs.readFile(__dirname + '/multi-key.xml', function (err, xml) {
fs.readFile(__dirname + '/multi-key.json', function (err, json) {
client.parse(xml, function (err, result) {
assert.ifError(err)
assert.deepEqual(result, JSON.parse(json))
done()
})
})
})
})
test('#parse() can parse error response', function (done){
var client = new Client()
fs.readFile(__dirname + '/error.xml', function (err, xml) {
client.parse(xml, function (err, result) {
assert.ok(err instanceof Error)
assert.equal(err.message, 'Must provide userID or keyID parameter for authentication.')
assert.equal(err.code, 106)
done()
})
})
})
test('#parse() can parse streams', function (done) {
var client = new Client()
, stream = fs.createReadStream(__dirname + '/simple.xml', {bufferSize: 128})
client.parse(stream, function (err, result) {
assert.ifError(err)
done()
})
})
test('#fetch() can request and parse API response', function (done) {
var client = new Client({url: 'http://127.0.0.1:1337'})
, server = http.createServer()
fs.readFile(__dirname + '/server-status.xml', function (err, xml) {
server.on('request', function (request, response) {
if (request.url === '/server/ServerStatus.xml.aspx') {
response.write(xml)
response.end()
}
})
server.listen(1337)
})
fs.readFile(__dirname + '/server-status.json', function (err, json) {
client.fetch('server:ServerStatus', function (err, result) {
assert.ifError(err)
assert.deepEqual(result, JSON.parse(json))
server.close()
done()
})
})
})
test('#fetch() only parses OK responses (status code == 200)', function (done) {
var client = new Client({url: 'http://127.0.0.1:1338'})
, server = http.createServer()
server.on('request', function (request, response) {
response.writeHead(404)
response.end()
})
server.listen(1338)
client.fetch('server:ServerStatus', function (err, result) {
assert.ok(err instanceof Error)
server.close()
done()
})
})