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('bbox_order.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 bbox', 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><changeset></changeset></osm>')
|
36 | })
|
37 |
|
38 | var uploaded = {}
|
39 | var SIZE = 100
|
40 | test('add docs to changeset', function (t) {
|
41 | t.plan(4)
|
42 | var docs = []
|
43 | for (var i = 0; i < SIZE; i++) {
|
44 | docs.push({
|
45 | type: 'node',
|
46 | id: 0 - i - 1,
|
47 | lat: 64 + i / SIZE,
|
48 | lon: -121 - i / SIZE,
|
49 | changeset: changeId
|
50 | })
|
51 | }
|
52 | docs.push({
|
53 | type: 'way',
|
54 | id: 0 - i - 1,
|
55 | changeset: changeId,
|
56 | refs: docs.map(function (d, i) { return 0 - (i + 1) })
|
57 | })
|
58 | var kdocs = {}
|
59 | docs.forEach(function (doc, i) {
|
60 | kdocs[0 - i - 1] = doc
|
61 | })
|
62 |
|
63 | var href = base + 'changeset/' + changeId + '/upload'
|
64 | var hq = hyperquest.post(href, {
|
65 | headers: { 'content-type': 'text/xml' }
|
66 | })
|
67 | hq.once('response', function (res) {
|
68 | t.equal(res.statusCode, 200)
|
69 | var contentObj = contentType.parse(res)
|
70 | t.equal(contentObj.type, 'text/xml', 'media type correct')
|
71 | t.equal(contentObj.parameters.charset.toLowerCase(), 'utf-8', 'charset correct')
|
72 | })
|
73 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
74 | var xml = parsexml(body)
|
75 | t.equal(xml.root.name, 'diffResult')
|
76 | xml.root.children.forEach(function (node, i) {
|
77 | var id = node.attributes.new_id
|
78 | uploaded[id] = kdocs[0 - i - 1]
|
79 | uploaded[id].id = id
|
80 | if (node.name === 'way') {
|
81 | uploaded[id].refs = uploaded[id].refs.map(function (ref) {
|
82 | return kdocs[ref].id
|
83 | })
|
84 | }
|
85 | })
|
86 | }))
|
87 | hq.end(`<osmChange version="1.0">
|
88 | <create>
|
89 | ${docs.map(function (doc) {
|
90 | return `<${doc.type} changeset="${doc.changeset}"
|
91 | id="${doc.id}"
|
92 | ${doc.lat ? `lat="${doc.lat}"` : ''}
|
93 | ${doc.lon ? `lon="${doc.lon}"` : ''}>
|
94 | ${(doc.refs || []).map(function (ref) {
|
95 | return `<nd ref="${ref}"/>`
|
96 | }).join('\n')}
|
97 | </${doc.type}>`
|
98 | }).join('\n')}
|
99 | </create>
|
100 | </osmChange>`)
|
101 | })
|
102 |
|
103 | test('bbox', function (t) {
|
104 | t.plan(6 + SIZE * 3)
|
105 | var href = base + 'map?bbox=-123,63,-120,66'
|
106 | var hq = hyperquest(href)
|
107 | hq.once('response', function (res) {
|
108 | t.equal(res.statusCode, 200)
|
109 | t.equal(res.headers['content-type'].split(/\s*;\s*/)[0], 'text/xml')
|
110 | })
|
111 | hq.pipe(concat({ encoding: 'string' }, function (body) {
|
112 | var xml = parsexml(body)
|
113 | t.equal(xml.root.name, 'osm')
|
114 | t.equal(xml.root.children[0].name, 'bounds')
|
115 | for (var i = 1; i < xml.root.children.length; i++) {
|
116 | var c = xml.root.children[i]
|
117 | var node = uploaded[c.attributes.id]
|
118 | if (c.name === 'node') {
|
119 | t.equal(c.attributes.changeset, node.changeset)
|
120 | t.equal(c.attributes.lat, String(node.lat))
|
121 | t.equal(c.attributes.lon, String(node.lon))
|
122 | } else if (c.name === 'way') {
|
123 | t.equal(c.children.length, SIZE, 'way')
|
124 | t.deepEqual(c.children.map(function (nd) {
|
125 | return nd.attributes.ref
|
126 | }), node.refs, 'way refs')
|
127 | }
|
128 | }
|
129 | }))
|
130 | })
|
131 |
|
132 | test('bbox_order.js: teardown server', function (t) {
|
133 | server.cleanup(function () {
|
134 | t.end()
|
135 | })
|
136 | })
|