Skip to content

Instantly share code, notes, and snippets.

@phillipkoebbe
Created June 3, 2011 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipkoebbe/1006206 to your computer and use it in GitHub Desktop.
Save phillipkoebbe/1006206 to your computer and use it in GitHub Desktop.
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>from@example.org</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>user1@example.org</to>
</delivery>
<delivery>
<to>user2@example.org</to>
</delivery>
<delivery>
<to>user3@example.org</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