aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/client_soap_test.rb
blob: ee02d919a43c41bf10ecac8abe9873f886af35e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require File.dirname(__FILE__) + '/abstract_client'


module ClientSoapTest
  PORT = 8998

  class SoapClientLet < ClientTest::AbstractClientLet
    def do_POST(req, res)
      test_request = ActionController::TestRequest.new
      test_request.request_parameters['action'] = req.path.gsub(/^\//, '').split(/\//)[1]
      test_request.env['REQUEST_METHOD'] = "POST"
      test_request.env['HTTP_CONTENTTYPE'] = 'text/xml'
      test_request.env['HTTP_SOAPACTION'] = req.header['soapaction'][0]
      test_request.env['RAW_POST_DATA'] = req.body
      response = ActionController::TestResponse.new
      @controller.process(test_request, response)
      res.header['content-type'] = 'text/xml'
      res.body = response.body
    rescue Exception => e
      $stderr.puts e.message
      $stderr.puts e.backtrace.join("\n")
    end
  end

  class ClientContainer < ActionController::Base
    web_client_api :client, :soap, "http://localhost:#{PORT}/client/api", :api => ClientTest::API
    web_client_api :invalid, :null, "", :api => true

    def get_client
      client
    end

    def get_invalid
      invalid
    end
  end

  class SoapServer < ClientTest::AbstractServer
    def create_clientlet(controller)
      SoapClientLet.new(controller)
    end

    def server_port
      PORT
    end
  end
end

class TC_ClientSoap < Test::Unit::TestCase
  include ClientTest
  include ClientSoapTest
  
  fixtures :users

  def setup
    @server = SoapServer.instance
    @container = @server.container
    @client = ActionWebService::Client::Soap.new(API, "http://localhost:#{@server.server_port}/client/api")
  end

  def test_void
    assert(@container.value_void.nil?)
    @client.void
    assert(!@container.value_void.nil?)
  end

  def test_normal
    assert(@container.value_normal.nil?)
    assert_equal(5, @client.normal(5, 6))
    assert_equal([5, 6], @container.value_normal)
    assert_equal(5, @client.normal("7", "8"))
    assert_equal([7, 8], @container.value_normal)
    assert_equal(5, @client.normal(true, false))
  end

  def test_array_return
    assert(@container.value_array_return.nil?)
    new_person = Person.new
    new_person.firstnames = ["one", "two"]
    new_person.lastname = "last"
    assert_equal([new_person], @client.array_return)
    assert_equal([new_person], @container.value_array_return)
  end

  def test_struct_pass
    assert(@container.value_struct_pass.nil?)
    new_person = Person.new
    new_person.firstnames = ["one", "two"]
    new_person.lastname = "last"
    assert_equal(true, @client.struct_pass([new_person]))
    assert_equal([[new_person]], @container.value_struct_pass)
  end
  
  def test_nil_struct_return
    assert_nil @client.nil_struct_return
  end
  
  def test_inner_nil
    outer = @client.inner_nil
    assert_equal 'outer', outer.name
    assert_nil outer.inner
  end

  def test_client_container
    assert_equal(50, ClientContainer.new.get_client.client_container)
    assert(ClientContainer.new.get_invalid.nil?)
  end

  def test_named_parameters
    assert(@container.value_named_parameters.nil?)
    assert(@client.named_parameters("key", 5).nil?)
    assert_equal(["key", 5], @container.value_named_parameters)
  end

  def test_capitalized_method_name
    @container.value_normal = nil
    assert_equal(5, @client.Normal(5, 6))
    assert_equal([5, 6], @container.value_normal)
    @container.value_normal = nil
  end
  
  def test_model_return
    user = @client.user_return
    assert_equal 1, user.id
    assert_equal 'Kent', user.name
    assert user.active?
    assert_kind_of Date, user.created_on
    assert_equal Date.today, user.created_on
  end
  
  def test_with_model
    with_model = @client.with_model_return
    assert_equal 'Kent', with_model.user.name
    assert_equal 2, with_model.users.size
    with_model.users.each do |user|
      assert_kind_of User, user
    end
  end
  
  def test_scoped_model_return
    scoped_model = @client.scoped_model_return
    assert_kind_of Accounting::User, scoped_model
    assert_equal 'Kent', scoped_model.name
  end
end