UNPKG

8.94 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 createServer = require('./lib/test_server.js')
8
9var base, server, changeId, changeId2
10
11test('split_way_delete.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 (1)', 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 </osm>`)
40})
41
42var ids = {}
43var versions = {}
44
45test('create way with changeset upload', function (t) {
46 t.plan(11)
47
48 var href = base + 'changeset/' + changeId + '/upload'
49 var hq = hyperquest.post(href, {
50 headers: { 'content-type': 'text/xml' }
51 })
52 hq.on('response', function (res) {
53 t.equal(res.statusCode, 200)
54 var contentObj = contentType.parse(res)
55 t.equal(contentObj.type, 'text/xml', 'media type correct')
56 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
57 })
58 hq.pipe(concat({ encoding: 'string' }, function (body) {
59 var xml = parsexml(body)
60 t.equal(xml.root.name, 'diffResult')
61 t.deepEqual(xml.root.children.map(function (c) {
62 return c.attributes.old_id
63 }).sort(), ['-1', '-2', '-3', '-4', '-5', '-6'])
64 xml.root.children.forEach(function (c) {
65 ids[c.attributes.old_id] = c.attributes.new_id
66 t.notEqual(c.attributes.old_id, c.attributes.new_id,
67 'placeholder id should not equal new id')
68 versions[c.attributes.old_id] = c.attributes.new_version
69 })
70 }))
71 hq.end(`<osmChange version="0.6" generator="osm-p2p-server test">
72 <create>
73 <node id="-1" lat="64.0" lon="-121.0" version="0" changeset="${changeId}"/>
74 <node id="-2" lat="64.1" lon="-121.1" version="0" changeset="${changeId}"/>
75 <node id="-3" lat="64.2" lon="-121.2" version="0" changeset="${changeId}"/>
76 <node id="-4" lat="64.3" lon="-121.3" version="0" changeset="${changeId}"/>
77 <node id="-5" lat="64.4" lon="-121.4" version="0" changeset="${changeId}"/>
78 <way id="-6" version="0" changeset="${changeId}">
79 <nd ref="-1"/>
80 <nd ref="-2"/>
81 <nd ref="-3"/>
82 <nd ref="-4"/>
83 <nd ref="-5"/>
84 </way>
85 </create>
86 </osmChange>`)
87})
88
89test('check way was correctly created', function (t) {
90 var href = base + 'map?bbox=-123,63,-120,66'
91 var hq = hyperquest(href)
92 hq.once('response', function (res) {
93 t.equal(res.statusCode, 200, 'response code correct')
94 var contentObj = contentType.parse(res)
95 t.equal(contentObj.type, 'text/xml', 'media type correct')
96 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
97 })
98 hq.pipe(concat({ encoding: 'string' }, function (body) {
99 var xml = parsexml(body)
100 t.equal(xml.root.name, 'osm')
101 t.equal(xml.root.children[0].name, 'bounds')
102 var nodes = xml.root.children.filter(c => c.name === 'node')
103 .sort((a, b) => Number(a.attributes.lat) - Number(b.attributes.lat))
104 var ways = xml.root.children.filter(c => c.name === 'way')
105 t.equal(nodes.length, 5, 'correct number of nodes')
106 t.equal(ways.length, 1, 'correct number of ways')
107 t.equal(ways[0].children.length, 5, 'way has correct number of nodes')
108 for (var i = 0; i < nodes.length; i++) {
109 t.equal(nodes[i].attributes.id, ids['-' + (i + 1)], 'ids match')
110 t.equal(Number(nodes[i].attributes.lat), 64 + i / 10, 'lat correct')
111 t.equal(ways[0].children[i].attributes.ref, ids['-' + (i + 1)], 'correct node in way')
112 }
113 t.end()
114 }))
115})
116
117test('create changeset (2)', function (t) {
118 t.plan(4)
119 var href = base + 'changeset/create'
120 var hq = hyperquest.put(href, {
121 headers: { 'content-type': 'text/xml' }
122 })
123 hq.once('response', function (res) {
124 t.equal(res.statusCode, 200, 'create 200 ok')
125 var contentObj = contentType.parse(res)
126 t.equal(contentObj.type, 'text/plain', 'media type correct')
127 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
128 })
129 hq.pipe(concat({ encoding: 'string' }, function (body) {
130 changeId2 = body.trim()
131 t.ok(/^[0-9A-Fa-f]+$/.test(changeId2), 'expected changeset id response')
132 }))
133 hq.end(`<osm>
134 <changeset>
135 <tag k="comment" v="whatever"/>
136 </changeset>
137 </osm>`)
138})
139
140test('split way and delete half changeset upload', function (t) {
141 t.plan(7)
142
143 var href = base + 'changeset/' + changeId2 + '/upload'
144 var hq = hyperquest.post(href, {
145 headers: { 'content-type': 'text/xml' }
146 })
147 hq.on('response', function (res) {
148 t.equal(res.statusCode, 200)
149 var contentObj = contentType.parse(res)
150 t.equal(contentObj.type, 'text/xml', 'media type correct')
151 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
152 })
153 hq.pipe(concat({ encoding: 'string' }, function (body) {
154 var xml = parsexml(body)
155 t.equal(xml.root.name, 'diffResult')
156 t.deepEqual(xml.root.children.map(function (c) {
157 return c.attributes.old_id
158 }).sort(), [ids['-6'], ids['-4'], ids['-5']].sort())
159 var deleted = xml.root.children.filter(c => !c.attributes.new_id)
160 t.deepEqual(deleted.map(c => c.attributes.old_id).sort(), [ids['-4'], ids['-5']].sort(), 'deleted correct')
161 var modified = xml.root.children.filter(c => c.attributes.new_id === c.attributes.old_id)
162 t.deepEqual(modified.map(c => c.attributes.old_id).sort(), [ids['-6']].sort(), 'modified correct')
163 versions.modified_way = modified[0].attributes.new_version
164 }))
165 hq.end(`<osmChange version="0.6" generator="iD">
166 <create/>
167 <modify>
168 <way id="${ids['-6']}" version="${versions['-6']}" changeset="${changeId2}">
169 <nd ref="${ids['-1']}"/>
170 <nd ref="${ids['-2']}"/>
171 <nd ref="${ids['-3']}"/>
172 </way>
173 </modify>
174 <delete if-unused="true">
175 <node id="${ids['-4']}" lat="64.3" lon="-121.3" version="${versions['-4']}" changeset="${changeId2}"/>
176 <node id="${ids['-5']}" lat="64.4" lon="-121.4" version="${versions['-5']}" changeset="${changeId2}"/>
177 </delete>
178 </osmChange>`)
179})
180
181test('Check modified way', function (t) {
182 var href = base + 'way/' + ids['-6'] + '?forks=true'
183 var hq = hyperquest(href)
184 hq.once('response', function (res) {
185 t.equal(res.statusCode, 200, 'response code correct')
186 var contentObj = contentType.parse(res)
187 t.equal(contentObj.type, 'text/xml', 'media type correct')
188 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
189 })
190 hq.pipe(concat({ encoding: 'string' }, function (body) {
191 var elements = parsexml(body).root.children
192 t.equal(elements.length, 1, 'no forks created')
193 t.equal(elements[0].attributes.changeset, changeId2)
194 var nodeIds = elements[0].children.map(function (c) { return c.attributes.ref })
195 t.deepEqual(nodeIds.sort(), [ids['-1'], ids['-2'], ids['-3']].sort())
196 t.end()
197 }))
198})
199
200test('check bbox with modified way', function (t) {
201 var href = base + 'map?bbox=-123,63,-120,66'
202 var hq = hyperquest(href)
203 hq.once('response', function (res) {
204 t.equal(res.statusCode, 200, 'response code correct')
205 var contentObj = contentType.parse(res)
206 t.equal(contentObj.type, 'text/xml', 'media type correct')
207 t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
208 })
209 hq.pipe(concat({ encoding: 'string' }, function (body) {
210 var xml = parsexml(body)
211 var nodes = xml.root.children.filter(c => c.name === 'node')
212 .sort((a, b) => Number(a.attributes.lat) - Number(b.attributes.lat))
213 var ways = xml.root.children.filter(c => c.name === 'way')
214 t.equal(nodes.length, 3, 'correct number of nodes')
215 t.equal(ways.filter(w => w.attributes.id === ids['-6']).length, 1, 'only one version of way in response')
216 t.equal(ways[0].children.length, 3, 'way has correct number of nodes')
217 for (var i = 0; i < ways[0].children.length; i++) {
218 t.equal(nodes[i].attributes.id, ids['-' + (i + 1)], 'ids match')
219 t.equal(Number(nodes[i].attributes.lat), 64 + i / 10, 'lat correct')
220 t.equal(ways[0].children[i].attributes.ref, ids['-' + (i + 1)], 'correct node in way')
221 }
222 t.end()
223 }))
224})
225
226test('split_way_delete.js: teardown server', function (t) {
227 server.cleanup(function () {
228 t.end()
229 })
230})