3 class CompressedRequestsTest < ActionDispatch::IntegrationTest
4 def test_no_compression
6 changeset = create(:changeset, :user => user)
10 relation = create(:relation)
11 other_relation = create(:relation)
12 # Create some tags, since we test that they are removed later
13 create(:node_tag, :node => node)
14 create(:way_tag, :way => way)
15 create(:relation_tag, :relation => relation)
17 # simple diff to change a node, way and relation by removing
19 diff = <<CHANGESET.strip_heredoc
22 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
23 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
24 <nd ref='#{node.id}'/>
28 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
29 <member type='way' role='some' ref='#{way.id}'/>
30 <member type='node' role='some' ref='#{node.id}'/>
31 <member type='relation' role='some' ref='#{other_relation.id}'/>
38 post "/api/0.6/changeset/#{changeset.id}/upload",
41 "HTTP_AUTHORIZATION" => format("Basic %{auth}", :auth => Base64.encode64("#{user.display_name}:test")),
42 "HTTP_CONTENT_TYPE" => "application/xml"
44 assert_response :success,
45 "can't upload an uncompressed diff to changeset: #{@response.body}"
47 # check that the changes made it into the database
48 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
49 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
50 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
53 def test_gzip_compression
55 changeset = create(:changeset, :user => user)
59 relation = create(:relation)
60 other_relation = create(:relation)
61 # Create some tags, since we test that they are removed later
62 create(:node_tag, :node => node)
63 create(:way_tag, :way => way)
64 create(:relation_tag, :relation => relation)
66 # simple diff to change a node, way and relation by removing
68 diff = <<CHANGESET.strip_heredoc
71 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
72 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
73 <nd ref='#{node.id}'/>
77 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
78 <member type='way' role='some' ref='#{way.id}'/>
79 <member type='node' role='some' ref='#{node.id}'/>
80 <member type='relation' role='some' ref='#{other_relation.id}'/>
87 post "/api/0.6/changeset/#{changeset.id}/upload",
88 :params => gzip_content(diff),
90 "HTTP_AUTHORIZATION" => format("Basic %{auth}", :auth => Base64.encode64("#{user.display_name}:test")),
91 "HTTP_CONTENT_ENCODING" => "gzip",
92 "HTTP_CONTENT_TYPE" => "application/xml"
94 assert_response :success,
95 "can't upload a gzip compressed diff to changeset: #{@response.body}"
97 # check that the changes made it into the database
98 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
99 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
100 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
103 def test_deflate_compression
105 changeset = create(:changeset, :user => user)
109 relation = create(:relation)
110 other_relation = create(:relation)
111 # Create some tags, since we test that they are removed later
112 create(:node_tag, :node => node)
113 create(:way_tag, :way => way)
114 create(:relation_tag, :relation => relation)
116 # simple diff to change a node, way and relation by removing
118 diff = <<CHANGESET.strip_heredoc
121 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
122 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
123 <nd ref='#{node.id}'/>
127 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
128 <member type='way' role='some' ref='#{way.id}'/>
129 <member type='node' role='some' ref='#{node.id}'/>
130 <member type='relation' role='some' ref='#{other_relation.id}'/>
137 post "/api/0.6/changeset/#{changeset.id}/upload",
138 :params => deflate_content(diff),
140 "HTTP_AUTHORIZATION" => format("Basic %{auth}", :auth => Base64.encode64("#{user.display_name}:test")),
141 "HTTP_CONTENT_ENCODING" => "deflate",
142 "HTTP_CONTENT_TYPE" => "application/xml"
144 assert_response :success,
145 "can't upload a deflate compressed diff to changeset: #{@response.body}"
147 # check that the changes made it into the database
148 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
149 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
150 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
153 def test_invalid_compression
155 changeset = create(:changeset, :user => user)
158 post "/api/0.6/changeset/#{changeset.id}/upload",
161 "HTTP_AUTHORIZATION" => format("Basic %{auth}", :auth => Base64.encode64("#{user.display_name}:test")),
162 "HTTP_CONTENT_ENCODING" => "unknown",
163 "HTTP_CONTENT_TYPE" => "application/xml"
165 assert_response :unsupported_media_type
170 def gzip_content(uncompressed)
171 compressed = StringIO.new
172 gz = Zlib::GzipWriter.new(compressed)
173 gz.write(uncompressed)
178 def deflate_content(uncompressed)
179 Zlib::Deflate.deflate(uncompressed)