UNPKG

4.85 kBJavaScriptView Raw
1var test = require('tape')
2var contentType = require('content-type')
3var parsexml = require('xml-parser')
4var hyperquest = require('hyperquest')
5var concat = require('concat-stream')
6
7var base, server, changeId
8
9var createServer = require('./lib/test_server.js')
10
11test('multi_fetch.js: setup server', function (t) {
12 createServer(function (d) {
13 base = d.base
14 server = d.server
15 t.end()
16 })
17})
18
19test('create changeset', function (t) {
20 t.plan(4)
21 var href = base + 'changeset/create'
22 var hq = hyperquest.put(href, {
23 headers: { 'content-type': 'text/xml' }
24 })
25 hq.once('response', function (res) {
26 t.equal(res.statusCode, 200, 'create 200 ok')
27 var contentObj = contentType.parse(res)
28 t.equal(contentObj.type, 'text/plain', 'media type correct')
29 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
30 })
31 hq.pipe(concat({ encoding: 'string' }, function (body) {
32 changeId = body.trim()
33 t.ok(/^[0-9A-Fa-f]+$/.test(changeId), 'expected changeset id response')
34 }))
35 hq.end(`<osm>
36 <changeset>
37 <tag k="comment" v="whatever"/>
38 </changeset>
39 <changeset>
40 <tag k="cool" v="beans"/>
41 <tag k="comment" v="wow"/>
42 </changeset>
43 </osm>`)
44})
45
46var uploaded = {}
47test('add docs to changeset', function (t) {
48 var docs = [
49 { type: 'node', lat: 64.5, lon: -121.5, changeset: changeId },
50 { type: 'node', lat: 63.9, lon: -120.9, changeset: changeId }
51 ]
52 t.plan(docs.length * 4)
53 docs.forEach(function (doc) {
54 var href = base + doc.type + '/create'
55 var hq = hyperquest.put(href, {
56 headers: { 'content-type': 'text/xml' }
57 })
58 hq.once('response', function (res) {
59 t.equal(res.statusCode, 200)
60 var contentObj = contentType.parse(res)
61 t.equal(contentObj.type, 'text/plain', 'media type correct')
62 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
63 })
64 hq.pipe(concat({ encoding: 'string' }, function (body) {
65 t.ok(/^[0-9A-Fa-f]+$/.test(body.trim()))
66 uploaded[doc.lon + ',' + doc.lat] = body.trim()
67 }))
68 hq.end(`<osm>
69 <node changeset="${doc.changeset}"
70 lat="${doc.lat}" lon="${doc.lon}"
71 id="IGNOREME">
72 </node>
73 </osm>`)
74 })
75})
76
77test('multi-fetch', function (t) {
78 t.plan(7)
79 var ids = Object.keys(uploaded)
80 .map(function (key) { return uploaded[key] })
81 var href = base + 'nodes?nodes=' + ids.join(',')
82 var hq = hyperquest(href, {
83 headers: { 'content-type': 'text/xml' }
84 })
85 hq.once('response', function (res) {
86 t.equal(res.statusCode, 200)
87 var contentObj = contentType.parse(res)
88 t.equal(contentObj.type, 'text/xml', 'media type correct')
89 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
90 })
91 hq.pipe(concat({ encoding: 'string' }, function (body) {
92 var xml = parsexml(body)
93 t.equal(xml.root.name, 'osm')
94 t.equal(xml.root.children[0].name, 'node')
95 t.equal(xml.root.children[1].name, 'node')
96 var xids = xml.root.children.map(function (x) {
97 return x.attributes.id
98 })
99 t.deepEqual(xids, ids, 'id comparison')
100 }))
101})
102
103test('multi-fetch random parameters in query string', function (t) {
104 t.plan(7)
105 var ids = Object.keys(uploaded)
106 .map(function (key) { return uploaded[key] })
107 var href = base + 'nodes?foo=bar&nodes=' + ids.join(',')
108 var hq = hyperquest(href, {
109 headers: { 'content-type': 'text/xml' }
110 })
111 hq.once('response', function (res) {
112 t.equal(res.statusCode, 200)
113 var contentObj = contentType.parse(res)
114 t.equal(contentObj.type, 'text/xml', 'media type correct')
115 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
116 })
117 hq.pipe(concat({ encoding: 'string' }, function (body) {
118 var xml = parsexml(body)
119 t.equal(xml.root.name, 'osm')
120 t.equal(xml.root.children[0].name, 'node')
121 t.equal(xml.root.children[1].name, 'node')
122 var xids = xml.root.children.map(function (x) {
123 return x.attributes.id
124 })
125 t.deepEqual(xids, ids, 'id comparison')
126 }))
127})
128
129test('multi-fetch error', function (t) {
130 t.plan(4)
131 var ids = Object.keys(uploaded)
132 .map(function (key) { return uploaded[key] })
133 var href = base + 'nodes?ways=' + ids.join(',')
134 var hq = hyperquest(href, {
135 headers: { 'content-type': 'text/xml' }
136 })
137 hq.once('response', function (res) {
138 t.equal(res.statusCode, 400)
139 var contentObj = contentType.parse(res)
140 t.equal(contentObj.type, 'text/plain', 'media type correct')
141 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
142 })
143 hq.pipe(concat({ encoding: 'string' }, function (body) {
144 t.equal(body.split('\n')[0], 'Missing parameter \'nodes\'.')
145 }))
146})
147
148test('multi_fetch.js: teardown server', function (t) {
149 server.cleanup(function () {
150 t.end()
151 })
152})