aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/abstract_client.rb
blob: 467c4e0d5b21f385ba5dd65fa5f8094920ded5ea (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require File.dirname(__FILE__) + '/abstract_unit'
require 'webrick'
require 'webrick/log'
require 'singleton'

module ClientTest
  class Person < ActionWebService::Struct
    member :firstnames, [:string]
    member :lastname,   :string

    def ==(other)
      firstnames == other.firstnames && lastname == other.lastname
    end
  end
  
  class Inner < ActionWebService::Struct
    member :name, :string
  end
  
  class Outer < ActionWebService::Struct
    member :name, :string
    member :inner, Inner
  end

  class User < ActiveRecord::Base
  end
  
  module Accounting
    class User < ActiveRecord::Base
    end
  end
  
  class WithModel < ActionWebService::Struct
    member :user, User
    member :users, [User]
  end
  
  class WithMultiDimArray < ActionWebService::Struct
    member :pref, [[:string]]
  end

  class API < ActionWebService::API::Base
    api_method :void
    api_method :normal,               :expects => [:int, :int], :returns => [:int]
    api_method :array_return,         :returns => [[Person]]
    api_method :struct_pass,          :expects => [[Person]], :returns => [:bool]
    api_method :nil_struct_return,    :returns => [Person] 
    api_method :inner_nil,            :returns => [Outer]
    api_method :client_container,     :returns => [:int]
    api_method :named_parameters,     :expects => [{:key=>:string}, {:id=>:int}]
    api_method :thrower
    api_method :user_return,          :returns => [User]
    api_method :with_model_return,    :returns => [WithModel]
    api_method :scoped_model_return,  :returns => [Accounting::User]
    api_method :multi_dim_return,     :returns => [WithMultiDimArray]
  end
  
  class NullLogOut
    def <<(*args); end
  end

  class Container < ActionController::Base
    web_service_api API

    attr_accessor :value_void
    attr_accessor :value_normal
    attr_accessor :value_array_return
    attr_accessor :value_struct_pass
    attr_accessor :value_named_parameters

    def initialize
      @value_void = nil
      @value_normal = nil
      @value_array_return = nil
      @value_struct_pass = nil
      @value_named_parameters = nil
    end

    def void
      @value_void = @method_params
    end

    def normal
      @value_normal = @method_params
      5
    end

    def array_return
      person = Person.new
      person.firstnames = ["one", "two"]
      person.lastname = "last"
      @value_array_return = [person]
    end

    def struct_pass
      @value_struct_pass = @method_params
      true
    end
    
    def nil_struct_return
      nil
    end
    
    def inner_nil
      Outer.new :name => 'outer', :inner => nil
    end

    def client_container
      50
    end

    def named_parameters
      @value_named_parameters = @method_params
    end

    def thrower
      raise "Hi"
    end
    
    def user_return
      User.find(1)
    end
    
    def with_model_return
      WithModel.new :user => User.find(1), :users => User.find(:all)
    end
    
    def scoped_model_return
      Accounting::User.find(1)
    end
    
    def multi_dim_return
      WithMultiDimArray.new :pref => [%w{pref1 value1}, %w{pref2 value2}]
    end
  end

  class AbstractClientLet < WEBrick::HTTPServlet::AbstractServlet
    def initialize(controller)
      @controller = controller
    end

    def get_instance(*args)
      self
    end

    def require_path_info?
      false
    end
  
    def do_GET(req, res)
      raise WEBrick::HTTPStatus::MethodNotAllowed, "GET request not allowed."
    end
  
    def do_POST(req, res)
      raise NotImplementedError
    end
  end

  class AbstractServer
    include ClientTest
    include Singleton
    attr :container
    def initialize
      @container = Container.new
      @clientlet = create_clientlet(@container)
      log = WEBrick::BasicLog.new(NullLogOut.new)
      @server = WEBrick::HTTPServer.new(:Port => server_port, :Logger => log, :AccessLog => log)
      @server.mount('/', @clientlet)
      @thr = Thread.new { @server.start }
      until @server.status == :Running; end
      at_exit { @server.stop; @thr.join }
    end
    
    protected
      def create_clientlet
        raise NotImplementedError
      end

      def server_port
        raise NotImplementedError
      end
  end
end