require File.dirname(__FILE__) + '/test_helper'
module EFaxTest
class RequestTest < Test::Unit::TestCase
def test_should_encode_data
EFax::Request.account_id = 1234567890
EFax::Request.publicize_class_methods do
assert_equal "id=1234567890&xml=%40abc%23&respond=XML", EFax::Request.params('@abc#')
end
end
end
class OutboundResponseTest < Test::Unit::TestCase
def test_successful_response
xml = <<-XML
12345678
1
Success
XML
http_response = mock
http_response.expects(:is_a?).with(Net::HTTPOK).returns(true)
http_response.expects(:body).returns(xml)
response = EFax::OutboundResponse.new(http_response)
assert_equal EFax::RequestStatus::SUCCESS, response.status_code
assert_equal "12345678", response.doc_id
assert_nil response.error_message
assert_nil response.error_level
end
def test_system_level_failed_response
xml = <<-XML
2
Failure
System
We FAIL
XML
http_response = mock()
http_response.expects(:is_a?).with(Net::HTTPOK).returns(true)
http_response.expects(:body).returns(xml)
response = EFax::OutboundResponse.new(http_response)
assert_equal EFax::RequestStatus::FAILURE, response.status_code
assert_nil response.doc_id
assert_equal "We FAIL", response.error_message
assert_equal "System", response.error_level
end
def test_user_level_failed_response
xml = <<-XML
2
Failure
User
"QWE RTY" does not satisfy the "base64Binary" type
XML
http_response = mock()
http_response.expects(:is_a?).with(Net::HTTPOK).returns(true)
http_response.expects(:body).returns(xml)
response = EFax::OutboundResponse.new(http_response)
assert_equal EFax::RequestStatus::FAILURE, response.status_code
assert_nil response.doc_id
assert_equal '"QWE RTY" does not satisfy the "base64Binary" type', response.error_message
assert_equal "User", response.error_level
end
def test_http_failed_response
http_response = mock()
http_response.expects(:is_a?).with(Net::HTTPOK).returns(false)
http_response.expects(:code).returns(500)
response = EFax::OutboundResponse.new(http_response)
assert_equal EFax::RequestStatus::HTTP_FAILURE, response.status_code
assert_nil response.doc_id
assert_equal "HTTP request failed (500)", response.error_message
assert_nil response.error_level
end
end
class OutboundStatusResponseTest < Test::Unit::TestCase
def test_successful_response
xml = <<-XML
12345678
Mike Rotch
Moe's
12345678901
Your transmission has completed.
"Success"
"Success"
08/06/2008
05:49:05
1
1
14400
0.4
1
"-"
XML
http_response = mock()
http_response.expects(:is_a?).with(Net::HTTPOK).returns(true)
http_response.expects(:body).returns(xml)
response = EFax::OutboundStatusResponse.new(http_response)
assert_equal "Your transmission has completed.", response.message
assert_equal "Success", response.outcome
assert_equal "Success", response.classification
assert_equal EFax::QueryStatus::SENT, response.status_code
end
def test_pending_response
xml = <<-XML
12345678
Mike Rotch
Moe's
12345678901
Your fax is waiting to be send
1
1
14400
0.4
1
"-"
XML
http_response = mock()
http_response.expects(:is_a?).with(Net::HTTPOK).returns(true)
http_response.expects(:body).returns(xml)
response = EFax::OutboundStatusResponse.new(http_response)
assert_equal "", response.outcome
assert_equal "", response.classification
assert_equal EFax::QueryStatus::PENDING, response.status_code
end
def test_failed_response
xml = <<-XML
12345678
Mike Rotch
Moe's
12345678901
Your fax caused the world to end
Apocalyptic failure
End of days
1
0
1
"-"
XML
http_response = mock()
http_response.expects(:is_a?).with(Net::HTTPOK).returns(true)
http_response.expects(:body).returns(xml)
response = EFax::OutboundStatusResponse.new(http_response)
assert_equal "Your fax caused the world to end", response.message
assert_equal "End of days", response.outcome
assert_equal "Apocalyptic failure", response.classification
assert_equal EFax::QueryStatus::FAILURE, response.status_code
end
end
class OutboundRequestTest < Test::Unit::TestCase
def test_generate_xml
expected_xml = <<-XML
Mike Rotch
moes
FINE
NORMAL
ENABLE
Subject
NONE
I. P. Freely
Moe's
12345678901
PGh0bWw+PGJvZHk+PGgxPlRlc3Q8L2gxPjwvYm9keT48L2h0bWw+
html
XML
EFax::Request.user = "Mike Rotch"
EFax::Request.password = "moes"
EFax::OutboundRequest.publicize_class_methods do
assert_equal expected_xml.delete(" "), EFax::OutboundRequest.xml("I. P. Freely", "Moe's", "12345678901", "Subject", "
Test
").delete(" ")
end
end
end
class OutboundStatusTest < Test::Unit::TestCase
def test_generate_xml
expected_xml = <<-XML
Mike Rotch
moes
123456
XML
EFax::OutboundStatus.publicize_class_methods do
assert_equal expected_xml.delete(" "), EFax::OutboundStatus.xml("123456").delete(" ")
end
end
end
class OutboundStatusTest < Test::Unit::TestCase
def test_generate_xml
expected_xml = <<-XML
Mike Rotch
moes
123456
XML
EFax::Request.user = "Mike Rotch"
EFax::Request.password = "moes"
EFax::OutboundStatus.publicize_class_methods do
assert_equal expected_xml.delete(" "), EFax::OutboundStatus.xml("123456").delete(" ")
end
end
end
end