Class: RubstApi::TestClient
- Inherits:
-
Object
- Object
- RubstApi::TestClient
- Defined in:
- lib/rubst_api/test_client.rb
Instance Method Summary collapse
-
#initialize(app, base_url: "http://testserver") ⇒ TestClient
constructor
A new instance of TestClient.
- #request(method, path, params: nil, json: UNDEFINED, data: nil, headers: {}, cookies: {}) ⇒ Object
Constructor Details
#initialize(app, base_url: "http://testserver") ⇒ TestClient
Returns a new instance of TestClient.
18 19 20 |
# File 'lib/rubst_api/test_client.rb', line 18 def initialize(app, base_url: "http://testserver") @app, @base_url = app, base_url end |
Instance Method Details
#request(method, path, params: nil, json: UNDEFINED, data: nil, headers: {}, cookies: {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rubst_api/test_client.rb', line 26 def request(method, path, params: nil, json: UNDEFINED, data: nil, headers: {}, cookies: {}) uri = URI.join(@base_url, path) query = URI.decode_www_form(uri.query.to_s) query.concat(params.flat_map { |key, value| Array(value).map { |item| [key.to_s, item.to_s] } }) if params body = if !json.equal?(UNDEFINED) headers = { "Content-Type" => "application/json" }.merge(headers) JSON.generate(json) elsif data headers = { "Content-Type" => "application/x-www-form-urlencoded" }.merge(headers) URI.encode_www_form(data) else "" end headers["Cookie"] = .map { |key, value| "#{key}=#{value}" }.join("; ") unless .empty? env = { "REQUEST_METHOD" => method.to_s.upcase, "PATH_INFO" => uri.path, "QUERY_STRING" => URI.encode_www_form(query), "rack.input" => StringIO.new(body), "rack.url_scheme" => uri.scheme, "HTTP_HOST" => uri.host, "CONTENT_LENGTH" => body.bytesize.to_s } headers.each do |key, value| env[key.to_s.downcase == "content-type" ? "CONTENT_TYPE" : "HTTP_#{key.to_s.upcase.tr("-", "_")}"] = value end TestResponse.new(*@app.call(env)) end |