Created
June 3, 2011 11:18
-
-
Save phillipkoebbe/1006206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: API Messages Create | |
Scenario: API key is not provided | |
Given a valid message | |
When I post the message without the api key | |
Then the response code should be 401 | |
================================================================= | |
class APIConsumer | |
require 'httparty' | |
include HTTParty | |
format :xml | |
def initialize(include_api_key) | |
@include_api_key = include_api_key | |
end | |
def headers | |
_headers = { | |
'Content-type' => 'application/xml', | |
'Accept' => 'text/xml, application/xml' | |
} | |
_headers['X-MiphMail-API-KEY'] = 'qwerty' if @include_api_key | |
_headers | |
end | |
def post(url, body) | |
self.class.post(url, :headers => self.headers, :body => body) | |
end | |
end | |
def message_xml(*parts_to_skip) | |
xml = '' | |
xml += '<?xml version="1.0" encoding="UTF-8"?>' unless parts_to_skip.include? :banner | |
xml += '<message>' | |
xml += '<from>[email protected]</from>' unless parts_to_skip.include? :from | |
xml += '<subject>Test Message</subject>' unless parts_to_skip.include? :subject | |
xml += '<body>This is the body of the test message.</body>' unless parts_to_skip.include? :body | |
deliveries = <<-DELIVERIES | |
<deliveries type="array"> | |
<delivery> | |
<to>[email protected]</to> | |
</delivery> | |
<delivery> | |
<to>[email protected]</to> | |
</delivery> | |
<delivery> | |
<to>[email protected]</to> | |
</delivery> | |
</deliveries> | |
DELIVERIES | |
xml += deliveries unless parts_to_skip.include? :deliveries | |
xml += '</message>' | |
xml | |
end | |
Given /^a valid message$/ do | |
@message = message_xml | |
end | |
Given /^I post the message (with|without) the api key$/ do |with_or_without| | |
api_consumer = APIConsumer.new(with_or_without == 'with') | |
api_url = "api/messages" | |
# this is where I need the scheme/port | |
@api_response = api_consumer.post(api_url, @message) | |
end | |
Given /^the response code should(?:\s(not)?)? be ([0-9]{3})$/ do |expectation, code| | |
if expectation == 'not' | |
@api_response.code.should_not == code.to_i | |
else | |
@api_response.code.should == code.to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment