UNPKG

5.57 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
10
11test('changeset_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', 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
42var ids = {}
43var versions = {}
44test('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 <nd ref="-2"/>
81 <nd ref="-3"/>
82 </way>
83 <node id="-6" changeset="${changeId}" lat="4.0" lon="8.0"/>
84 </create>
85 </osmChange>`)
86})
87
88test('rejected delete', function (t) {
89 t.plan(2)
90
91 var href = base + 'changeset/' + changeId + '/upload'
92 var hq = hyperquest.post(href, {
93 headers: { 'content-type': 'text/xml' }
94 })
95 hq.on('response', function (res) {
96 t.notEqual(res.statusCode, 200)
97 })
98 hq.pipe(concat({ encoding: 'string' }, function (body) {
99 t.true(body.includes('Element #' + ids['-1'] + ' is still used by element #' + ids['-4'] + '.'))
100 }))
101 hq.end(`<osmChange version="1.0" generator="acme osm editor">
102 <delete>
103 <node id="${ids['-1']}" changeset="${changeId}"/>
104 </delete>
105 </osmChange>`)
106})
107
108test('accepted delete', function (t) {
109 t.plan(2)
110
111 var href = base + 'changeset/' + changeId + '/upload'
112 var hq = hyperquest.post(href, {
113 headers: { 'content-type': 'text/xml' }
114 })
115 hq.on('response', function (res) {
116 t.equal(res.statusCode, 200)
117 })
118 hq.pipe(concat({ encoding: 'string' }, function (body) {
119 var xml = parsexml(body)
120 t.equal(xml.root.name, 'diffResult')
121 }))
122 hq.end(`<osmChange version="1.0" generator="acme osm editor">
123 <delete>
124 <node id="${ids['-1']}" changeset="${changeId}"/>
125 <way id="${ids['-4']}" changeset="${changeId}"></way>
126 </delete>
127 </osmChange>`)
128})
129
130test('conditional delete', function (t) {
131 t.plan(2)
132
133 var href = base + 'changeset/' + changeId + '/upload'
134 var hq = hyperquest.post(href, {
135 headers: { 'content-type': 'text/xml' }
136 })
137 hq.on('response', function (res) {
138 t.equal(res.statusCode, 200)
139 })
140 hq.pipe(concat({ encoding: 'string' }, function (body) {
141 var xml = parsexml(body)
142 t.equal(xml.root.name, 'diffResult')
143 }))
144 hq.end(`<osmChange version="1.0" generator="acme osm editor">
145 <delete if-unused="1">
146 <node id="${ids['-3']}" changeset="${changeId}"/>
147 <node id="${ids['-6']}" changeset="${changeId}"/>
148 </delete>
149 </osmChange>`)
150})
151
152test('list documents', function (t) {
153 t.plan(5)
154
155 var href = base + 'map?bbox=0,0,10,10'
156 var hq = hyperquest(href, {
157 headers: { 'content-type': 'text/xml' }
158 })
159 hq.on('response', function (res) {
160 t.equal(res.statusCode, 200)
161 t.equal(res.headers['content-type'].split(/\s*;/)[0], 'text/xml')
162 })
163 hq.pipe(concat({ encoding: 'string' }, function (body) {
164 var xml = parsexml(body)
165 t.equal(xml.root.name, 'osm')
166 t.deepEqual(xml.root.children[0], {
167 name: 'bounds',
168 attributes: { minlat: '0', maxlat: '10', minlon: '0', maxlon: '10' },
169 children: []
170 }, 'bounds')
171 var rids = xml.root.children.slice(1).map(function (c) {
172 return c.attributes.id
173 }).sort()
174 t.deepEqual(rids, [
175 ids['-2'], ids['-3'], ids['-5']
176 ].sort(), 'undeleted documents')
177 }))
178})
179
180test('changeset_delete.js: teardown server', function (t) {
181 server.cleanup(function () {
182 t.end()
183 })
184})