Class: Nyara::Test::Env

Inherits:
Struct
  • Object
show all
Defined in:
lib/nyara/test.rb,
lib/nyara/test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_size_limit = 5_000_000) ⇒ Env

Call-seq

# change size limit of response data to 100M:
@_env = Env.new 10**8

26
27
28
29
30
# File 'lib/nyara/test.rb', line 26

def initialize response_size_limit=5_000_000
  self.response_size_limit = response_size_limit
  self.cookie = ParamHash.new
  self.session = Session.new
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller

Returns:

  • (Object)

    the current value of controller


19
20
21
# File 'lib/nyara/test.rb', line 19

def controller
  @controller
end

Returns the value of attribute cookie

Returns:

  • (Object)

    the current value of cookie


19
20
21
# File 'lib/nyara/test.rb', line 19

def cookie
  @cookie
end

#requestObject

Returns the value of attribute request

Returns:

  • (Object)

    the current value of request


19
20
21
# File 'lib/nyara/test.rb', line 19

def request
  @request
end

#responseObject

Returns the value of attribute response

Returns:

  • (Object)

    the current value of response


19
20
21
# File 'lib/nyara/test.rb', line 19

def response
  @response
end

#response_size_limitObject

Returns the value of attribute response_size_limit

Returns:

  • (Object)

    the current value of response_size_limit


19
20
21
# File 'lib/nyara/test.rb', line 19

def response_size_limit
  @response_size_limit
end

#sessionObject

Returns the value of attribute session

Returns:

  • (Object)

    the current value of session


19
20
21
# File 'lib/nyara/test.rb', line 19

def session
  @session
end

Instance Method Details

#http(meth, path, headers = {}, body_string_or_hash = '') ⇒ Object


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
# File 'lib/nyara/test.rb', line 62

def http meth, path, headers={}, body_string_or_hash=''
  headers = (headers || {}).dup

  # serialize body
  # todo build multipart for file
  if body_string_or_hash.is_a?(Hash)
    body = body_string_or_hash.to_param
    headers['Content-Type'] = 'application/x-www-form-urlencoded'
  else
    body = body_string_or_hash.to_s
    headers['Content-Type'] ||= 'text/plain'
  end
  if body.bytesize > 0
    headers['Content-Length'] = body.bytesize
  end

  # serialize cookie / session
  if headers['Cookie']
    cookie.clear
    cookie.merge! Cookie.decode headers
  end
  Session.encode_to_cookie session, cookie
  headers['Cookie'] = Cookie.encode cookie

  request_data = ["#{meth.to_s.upcase} #{Ext.escape path, true} HTTP/1.1\r\n"]
  headers.each do |k, v|
    request_data << "#{k}: #{v}\r\n"
  end
  request_data << "\r\n"
  request_data << body
  process_request_data request_data.join
end

#process_request_data(data) ⇒ Object


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
# File 'lib/nyara/test.rb', line 32

def process_request_data data
  client, server = Socket.pair :UNIX, :STREAM
  self.request = Ext.request_new
  Ext.request_set_fd request, server.fileno

  client << data
  self.controller = Ext.handle_request request
  # response_data can be empty
  response_data = (client.read_nonblock response_size_limit rescue '')
  self.response = Response.new response_data

  # no env when route not found
  if request.session
    # merge session
    session.clear
    session.merge! request.session

    # merge Set-Cookie
    response.set_cookies.each do |cookie_seg|
      # todo distinguish delete, value and set
      ParamHash.parse_cookie cookie, cookie_seg
    end
  end

ensure
  Ext.request_unset_fd request
  server.close
  client.close
end