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 createServer = require('./lib/test_server.js')
|
8 |
|
9 | var base, server, changeId
|
10 |
|
11 | test('changeset.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="whatever"/>
|
38 | </changeset>
|
39 | <changeset>
|
40 | <tag k="cool" v="beans"/>
|
41 | <tag k="comment" v="wow"/>
|
42 | </changeset>
|
43 | </osm>`)
|
44 | })
|
45 |
|
46 | test('get empty osmchange doc', function (t) {
|
47 | t.plan(5)
|
48 | var href = base + 'changeset/' + changeId + '/download'
|
49 | var hq = hyperquest(href, {
|
50 | headers: { 'content-type': 'text/xml' }
|
51 | })
|
52 | hq.once('response', function (res) {
|
53 | t.equal(res.statusCode, 200, 'create 200 ok')
|
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, 'osmChange')
|
61 | t.equal(xml.root.children.length, 0)
|
62 | }))
|
63 | })
|
64 |
|
65 | var uploaded = {}
|
66 | test('add docs to changeset', function (t) {
|
67 | var docs = [
|
68 | { type: 'node', lat: 64.5, lon: -121.5, changeset: changeId },
|
69 | { type: 'node', lat: 63.9, lon: -120.9, changeset: changeId }
|
70 | ]
|
71 | t.plan(docs.length * 4)
|
72 | docs.forEach(function (doc) {
|
73 | var href = base + doc.type + '/create'
|
74 | var hq = hyperquest.put(href, {
|
75 | headers: { 'content-type': 'text/xml' }
|
76 | })
|
77 | hq.once('response', function (res) {
|
78 | t.equal(res.statusCode, 200)
|
79 | var contentObj = contentType.parse(res)
|
80 | t.equal(contentObj.type, 'text/plain', 'media type correct')
|
81 | t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
|
82 | })
|
83 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
84 | t.ok(/^[0-9A-Fa-f]+$/.test(body.trim()))
|
85 | uploaded[doc.lon + ',' + doc.lat] = body.trim()
|
86 | }))
|
87 | hq.end(`<osm>
|
88 | <node changeset="${doc.changeset}"
|
89 | lat="${doc.lat}" lon="${doc.lon}"
|
90 | id="IGNOREME">
|
91 | </node>
|
92 | </osm>`)
|
93 | })
|
94 | })
|
95 |
|
96 | var versions = {}
|
97 | test('get doc versions', function (t) {
|
98 | t.plan(8)
|
99 | Object.keys(uploaded).forEach(function (key) {
|
100 | var href = base + 'node/' + uploaded[key]
|
101 | var hq = hyperquest(href)
|
102 | hq.once('response', function (res) {
|
103 | t.equal(res.statusCode, 200)
|
104 | var contentObj = contentType.parse(res)
|
105 | t.equal(contentObj.type, 'text/xml', 'media type correct')
|
106 | t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
|
107 | })
|
108 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
109 | var xml = parsexml(body)
|
110 | t.equal(xml.root.name, 'osm')
|
111 |
|
112 | xml.root.children.forEach(function (c) {
|
113 | versions[key] = c.attributes.version
|
114 | })
|
115 | }))
|
116 | })
|
117 | })
|
118 |
|
119 | test('get osmchange doc', function (t) {
|
120 | t.plan(4)
|
121 | var href = base + 'changeset/' + changeId + '/download'
|
122 | var hq = hyperquest(href, {
|
123 | headers: { 'content-type': 'text/xml' }
|
124 | })
|
125 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
126 | var xml = parsexml(body)
|
127 | t.equal(xml.root.name, 'osmChange')
|
128 | t.equal(xml.root.children.length, 1)
|
129 | t.equal(xml.root.children[0].name, 'create')
|
130 | xml.root.children[0].children.forEach(function (c) {
|
131 | delete c.attributes.timestamp
|
132 | })
|
133 | t.deepEqual(xml.root.children[0].children.sort(cmpch), [
|
134 | {
|
135 | name: 'node',
|
136 | attributes: {
|
137 | id: uploaded['-121.5,64.5'],
|
138 | version: versions['-121.5,64.5'],
|
139 | changeset: changeId,
|
140 | lat: '64.5',
|
141 | lon: '-121.5'
|
142 | },
|
143 | children: []
|
144 | },
|
145 | {
|
146 | name: 'node',
|
147 | attributes: {
|
148 | id: uploaded['-120.9,63.9'],
|
149 | version: versions['-120.9,63.9'],
|
150 | changeset: changeId,
|
151 | lat: '63.9',
|
152 | lon: '-120.9'
|
153 | },
|
154 | children: []
|
155 | }
|
156 | ].sort(cmpch))
|
157 | }))
|
158 | })
|
159 |
|
160 | test('changeset.js: teardown server', function (t) {
|
161 | server.cleanup(function () {
|
162 | t.end()
|
163 | })
|
164 | })
|
165 |
|
166 | function cmpch (a, b) {
|
167 | return a.attributes.id < b.attributes.id ? -1 : 1
|
168 | }
|