features/step_definitions/httpthumbnailer_steps.rb in httpthumbnailer-0.3.1 vs features/step_definitions/httpthumbnailer_steps.rb in httpthumbnailer-1.0.0
- old
+ new
@@ -1,107 +1,152 @@
-Given /httpthumbnailer log is empty/ do
- (support_dir + 'server.log').truncate(0)
-end
-
Given /httpthumbnailer server is running at (.*)/ do |url|
- start_server(
- "bundle exec #{script('httpthumbnailer')}",
- '/tmp/httpthumbnailer.pid',
- support_dir + 'server.log',
- url
- )
+ log = support_dir + 'server.log'
+ cmd = "bundle exec #{script('httpthumbnailer')} -f -d -l #{log} -w 1"
+ start_server(cmd, '/tmp/httpthumbnailer.pid', log, url)
end
Given /(.*) file content as request body/ do |file|
@request_body = File.open(support_dir + file){|f| f.read }
end
When /I do (.*) request (.*)/ do |method, url|
- @response = HTTPClient.new.request(method, url, nil, @request_body)
+ @response = http_client.request(method, url, nil, @request_body)
end
-Then /(.*) header will be (.*)/ do |header, value|
+When /I save response body/ do
+ @saved_response_body = @response.body
+end
+
+Then /(.*) header should be (.*)/ do |header, value|
@response.header[header].should_not be_empty
@response.header[header].first.should == value
end
-Then /I will get multipart response/ do
+Then /I should get multipart response/ do
@response.header['Content-Type'].first.should match /^multipart/
- @response_multipart = MultipartResponse.new(@response.header['Content-Type'].last, @response.body)
+ parser = MultipartParser::Reader.new(MultipartParser::Reader.extract_boundary_value(@response.header['Content-Type'].last))
+ @response_multipart = []
+
+ parser.on_part do |part|
+ part_struct = OpenStruct.new
+ part_struct.headers = part.headers
+
+ part_struct.body = ''
+ part.on_data do |data|
+ part_struct.body << data
+ end
+
+ part.on_end do
+ @response_multipart << part_struct
+ end
+ end
+
+ parser.write @response.body
+
+ parser.ended?.should be_true
+ @response_multipart.should_not be_empty
end
-Then /response body will be CRLF endend lines like/ do |body|
+Then /response body should be CRLF endend lines like/ do |body|
@response.body.should match(body)
- @response.body.each do |line|
+ @response.body.each_line do |line|
line[-2,2].should == "\r\n"
end
end
-Then /response body will be CRLF endend lines$/ do |body|
+Then /response body should be CRLF endend lines$/ do |body|
@response.body.should == body.gsub("\n", "\r\n") + "\r\n"
end
-Then /response status will be (.*)/ do |status|
+Then /response status should be (.*)/ do |status|
@response.status.should == status.to_i
end
-Then /response content type will be (.*)/ do |content_type|
+Then /response content type should be (.*)/ do |content_type|
@response.header['Content-Type'].first.should == content_type
end
-Then /(.*) part mime type will be (.*)/ do |part, mime|
- @response_multipart.part[part_no(part)].header['Content-Type'].should == mime
+Then /response mime type should be (.*)/ do |mime_type|
+ step "response content type should be #{mime_type}"
end
-Then /(.*) part content type will be (.*)/ do |part, content_type|
- @response_multipart.part[part_no(part)].header['Content-Type'].should == content_type
+Then /(.*) part mime type should be (.*)/ do |part, mime|
+ @response_multipart[part_no(part)].headers['content-type'].should == mime
end
-Then /(.*) part body will be CRLF endend lines$/ do |part, body|
- @response_multipart.part[part_no(part)].body.should == body.gsub("\n", "\r\n") + "\r\n"
+Then /(.*) part content type should be (.*)/ do |part, content_type|
+ @response_multipart[part_no(part)].headers['content-type'].should == content_type
end
-Then /(.*) part body will be CRLF endend lines like$/ do |part, body|
- pbody = @response_multipart.part[part_no(part)].body
+Then /(.*) part status should be (.*)/ do |part, status|
+ @response_multipart[part_no(part)].headers['status'].should == status
+end
+
+Then /(.*) part body should be CRLF endend lines$/ do |part, body|
+ @response_multipart[part_no(part)].body.should == body.gsub("\n", "\r\n")
+end
+
+Then /(.*) part body should be CRLF endend lines like$/ do |part, body|
+ pbody = @response_multipart[part_no(part)].body
pbody.should match(body)
- pbody.each do |line|
- line[-2,2].should == "\r\n"
- end
end
-Then /(.*) part will contain (.*) image of size (.*)x(.*)/ do |part, format, width, height|
- mime = @response_multipart.part[part_no(part)].header['Content-Type']
- data = @response_multipart.part[part_no(part)].body
+Then /response should contain (.*) image of size (.*)x(.*)/ do |format, width, height|
+ mime = @response.header['Content-Type'].first
+ data = @response.body
fail("expecte image got #{mime}: #{data}") unless mime =~ /^image\//
@image.destroy! if @image
@image = Magick::Image.from_blob(data).first
@image.format.should == format
@image.columns.should == width.to_i
@image.rows.should == height.to_i
end
-Then /(.*) part will contain body of size within (.*) of (.*)/ do |part, margin, size|
- data = @response_multipart.part[part_no(part)].body
- data.length.should be_within(margin.to_i).of(size.to_i)
+
+Then /(.*) part should contain (.*) image of size (.*)x(.*)/ do |part, format, width, height|
+ mime = @response_multipart[part_no(part)].headers['content-type']
+ data = @response_multipart[part_no(part)].body
+
+ mime.should match /^image\//
+ data.should_not be_empty
+
+ @image.destroy! if @image
+ @image = Magick::Image.from_blob(data).first
+
+ @image.format.should == format
+ @image.columns.should == width.to_i
+ @image.rows.should == height.to_i
end
-Then /(.*) part will contain body smaller than (.*) part/ do |part, big_part|
- data = @response_multipart.part[part_no(part)].body
- data_big = @response_multipart.part[part_no(big_part)].body
- data.length.should < data_big.length
+Then /saved response body will be smaller than response body/ do
+ @saved_response_body.length.should < @response.body.length
end
+And /response will be saved as (.*) for human inspection/ do |file|
+ data = @response.body
+ (support_dir + file).open('w'){|f| f.write(data)}
+end
+
And /(.*) part body will be saved as (.*) for human inspection/ do |part, file|
- data = @response_multipart.part[part_no(part)].body
+ data = @response_multipart[part_no(part)].body
(support_dir + file).open('w'){|f| f.write(data)}
end
-And /that image pixel at (.*)x(.*) will be of color (.*)/ do |x, y, color|
+And /that image pixel at (.*)x(.*) should be of color (.*)/ do |x, y, color|
@image.pixel_color(x.to_i, y.to_i).to_color.sub(/^#/, '0x').should == color
end
-And /there will be no leaked images/ do
- HTTPClient.new.get_content("http://localhost:3100/stats/images").to_i.should == 0
+And /that image should be (.*) bit image/ do |bits|
+ @image.depth.should == bits.to_i
+end
+
+
+And /there should be no leaked images/ do
+ Integer(http_client.get_content("http://localhost:3100/stats/images_loaded").strip).should == 0
+end
+
+And /there should be maximum (.*) images loaded during single request/ do |max|
+ Integer(http_client.get_content("http://localhost:3100/stats/max_images_loaded").strip).should <= max.to_i
end