1 | var test = require('tape')
|
2 | var contentType = require('content-type')
|
3 | var parsexml = require('xml-parser')
|
4 | var hyperquest = require('hyperquest')
|
5 | var concat = require('concat-stream')
|
6 |
|
7 | var base, server, changeId
|
8 |
|
9 | var createServer = require('./lib/test_server.js')
|
10 |
|
11 | test('delete.js: setup server', function (t) {
|
12 | createServer(function (d) {
|
13 | base = d.base
|
14 | server = d.server
|
15 | t.end()
|
16 | })
|
17 | })
|
18 |
|
19 | test('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="wow"/>
|
38 | </changeset>
|
39 | </osm>`)
|
40 | })
|
41 |
|
42 | var ids = {}
|
43 | var versions = {}
|
44 | test('add docs', function (t) {
|
45 | t.plan(5 + 6)
|
46 |
|
47 | var href = base + 'changeset/' + changeId + '/upload'
|
48 | var hq = hyperquest.post(href, {
|
49 | headers: { 'content-type': 'text/xml' }
|
50 | })
|
51 | hq.on('response', function (res) {
|
52 | t.equal(res.statusCode, 200)
|
53 | var contentObj = contentType.parse(res)
|
54 | t.equal(contentObj.type, 'text/xml', 'media type correct')
|
55 | t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
|
56 | })
|
57 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
58 | var xml = parsexml(body)
|
59 | t.equal(xml.root.name, 'diffResult')
|
60 | t.deepEqual(xml.root.children.map(function (c) {
|
61 | return c.attributes.old_id
|
62 | }).sort(), ['-1', '-2', '-3', '-4', '-5', '-6'])
|
63 | xml.root.children.forEach(function (c) {
|
64 | ids[c.attributes.old_id] = c.attributes.new_id
|
65 | t.notEqual(c.attributes.old_id, c.attributes.new_id,
|
66 | 'placeholder id should not equal new id')
|
67 | versions[c.attributes.old_id] = c.attributes.new_version
|
68 | })
|
69 | }))
|
70 | hq.end(`<osmChange version="1.0" generator="acme osm editor">
|
71 | <create>
|
72 | <node id="-1" changeset="${changeId}" lat="1.0" lon="5.0"/>
|
73 | <node id="-2" changeset="${changeId}" lat="2.0" lon="6.0"/>
|
74 | <node id="-3" changeset="${changeId}" lat="3.0" lon="7.0"/>
|
75 | <way id="-4" changeset="${changeId}">
|
76 | <nd ref="-1"/>
|
77 | <nd ref="-2"/>
|
78 | </way>
|
79 | <way id="-5" changeset="${changeId}">
|
80 |
|
81 | <nd ref="-3"/>
|
82 | </way>
|
83 | <node id="-6" changeset="${changeId}" lat="4.0" lon="8.0"/>
|
84 | </create>
|
85 | </osmChange>`)
|
86 | })
|
87 |
|
88 | test('rejected delete', function (t) {
|
89 | t.plan(2)
|
90 |
|
91 | var href = base + 'node/' + ids['-1']
|
92 | var hq = hyperquest.post(href, {
|
93 | headers: {
|
94 | 'content-type': 'text/xml',
|
95 | X_HTTP_METHOD_OVERRIDE: 'DELETE'
|
96 | }
|
97 | })
|
98 | hq.on('response', function (res) {
|
99 | t.notEqual(res.statusCode, 200)
|
100 | })
|
101 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
102 | t.true(body.includes('Element #' + ids['-1'] + ' is still used by element #' + ids['-4'] + '.'))
|
103 | }))
|
104 | hq.end(`<osm><node id="${ids['-1']}" changeset="${changeId}"/></osm>`)
|
105 | })
|
106 |
|
107 | test('list documents', function (t) {
|
108 | t.plan(5)
|
109 |
|
110 | var href = base + 'map?bbox=0,0,10,10'
|
111 | var hq = hyperquest(href, {
|
112 | headers: { 'content-type': 'text/xml' }
|
113 | })
|
114 | hq.on('response', function (res) {
|
115 | t.equal(res.statusCode, 200)
|
116 | t.equal(res.headers['content-type'].split(/\s*;/)[0], 'text/xml')
|
117 | })
|
118 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
119 | var xml = parsexml(body)
|
120 | t.equal(xml.root.name, 'osm')
|
121 | t.deepEqual(xml.root.children[0], {
|
122 | name: 'bounds',
|
123 | attributes: { minlat: '0', maxlat: '10', minlon: '0', maxlon: '10' },
|
124 | children: []
|
125 | }, 'bounds')
|
126 | var rids = xml.root.children.slice(1).map(function (c) {
|
127 | return c.attributes.id
|
128 | }).sort()
|
129 | t.deepEqual(rids, [
|
130 | ids['-1'], ids['-2'], ids['-3'], ids['-4'], ids['-5'], ids['-6']
|
131 | ].sort(), 'undeleted documents')
|
132 | }))
|
133 | })
|
134 |
|
135 | test('accepted delete way', function (t) {
|
136 | t.plan(2)
|
137 |
|
138 | var href = base + 'way/' + ids['-4']
|
139 | var hq = hyperquest.post(href, {
|
140 | headers: {
|
141 | 'content-type': 'text/xml',
|
142 | X_HTTP_METHOD_OVERRIDE: 'DELETE'
|
143 | }
|
144 | })
|
145 | hq.on('response', function (res) {
|
146 | t.equal(res.statusCode, 200)
|
147 | })
|
148 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
149 | t.ok(/^[0-9A-Fa-f]{8,}$/.test(body.trim()), 'version response')
|
150 | }))
|
151 | hq.end(`<osm><way id="${ids['-4']}" changeset="${changeId}"/></osm>`)
|
152 | })
|
153 |
|
154 | test('accepted delete node', function (t) {
|
155 | t.plan(2)
|
156 |
|
157 | var href = base + 'node/' + ids['-1']
|
158 | var hq = hyperquest.post(href, {
|
159 | headers: {
|
160 | 'content-type': 'text/xml',
|
161 | X_HTTP_METHOD_OVERRIDE: 'DELETE'
|
162 | }
|
163 | })
|
164 | hq.on('response', function (res) {
|
165 | t.equal(res.statusCode, 200)
|
166 | })
|
167 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
168 | t.ok(/^[0-9A-Fa-f]{8,}$/.test(body.trim()), 'version response')
|
169 | }))
|
170 | hq.end(`<osm><node id="${ids['-1']}" changeset="${changeId}"/></osm>`)
|
171 | })
|
172 |
|
173 | test('list documents', function (t) {
|
174 | t.plan(5)
|
175 |
|
176 | var href = base + 'map?bbox=0,0,10,10'
|
177 | var hq = hyperquest(href, {
|
178 | headers: { 'content-type': 'text/xml' }
|
179 | })
|
180 | hq.on('response', function (res) {
|
181 | t.equal(res.statusCode, 200)
|
182 | t.equal(res.headers['content-type'].split(/\s*;/)[0], 'text/xml')
|
183 | })
|
184 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
185 | var xml = parsexml(body)
|
186 | t.equal(xml.root.name, 'osm')
|
187 | t.deepEqual(xml.root.children[0], {
|
188 | name: 'bounds',
|
189 | attributes: { minlat: '0', maxlat: '10', minlon: '0', maxlon: '10' },
|
190 | children: []
|
191 | }, 'bounds')
|
192 | var rids = xml.root.children.slice(1).map(function (c) {
|
193 | return c.attributes.id
|
194 | }).sort()
|
195 | t.deepEqual(rids, [
|
196 | ids['-3'], ids['-5'], ids['-6']
|
197 | ].sort(), 'undeleted documents')
|
198 | }))
|
199 | })
|
200 |
|
201 | test('rejected delete type mismatch', function (t) {
|
202 | t.plan(1)
|
203 |
|
204 | var href = base + 'way/' + ids['-2']
|
205 | var hq = hyperquest.post(href, {
|
206 | headers: {
|
207 | 'content-type': 'text/xml',
|
208 | X_HTTP_METHOD_OVERRIDE: 'DELETE'
|
209 | }
|
210 | })
|
211 | hq.on('response', function (res) {
|
212 | t.equal(res.statusCode, 400)
|
213 | })
|
214 | hq.end(`<osm><node id="${ids['-2']} changeset="${changeId}""/></osm>`)
|
215 | })
|
216 |
|
217 | test('delete.js: teardown server', function (t) {
|
218 | server.cleanup(function () {
|
219 | t.end()
|
220 | })
|
221 | })
|