module KnowsAboutApiRequests
Constants
- CONTENT_TYPE
Public Instance Methods
http_client()
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 4 def http_client @http ||= HTTPClient.new(TEST_CONFIG["proxy"]) # Ensure we're using the system SSL certs, as per other libraries (like HTTParty) @http.ssl_config.set_trust_ca(OpenSSL::X509::DEFAULT_CERT_FILE) @http.debug_dev = STDOUT if TEST_CONFIG["debug"] #@http.reset_all @http end
http_delete(server, path, header = {})
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 72 def http_delete(server, path, header = {}) uri = qualified_uri(server, path) params = query if query.count > 0 @response = http_client.delete(uri, query: params, header: request_headers(header)) end
http_get(server, path, header = {})
click to toggle source
request methods
# File lib/cucumber/blinkbox/requests.rb, line 56 def http_get(server, path, header = {}) uri = qualified_uri(server, path) params = query if query.count > 0 @response = http_client.get(uri, query: params, header: request_headers(header), follow_redirect: true) end
http_post(server, path, body = {}, header = {})
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 62 def http_post(server, path, body = {}, header = {}) uri = qualified_uri(server, path) @response = http_client.post(uri, body: format_body(body), header: request_headers({"Content-Type" => CONTENT_TYPE}.merge(header))) end
http_put(server, path, body = {}, header = {})
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 67 def http_put(server, path, body = {}, header = {}) uri = qualified_uri(server, path) @response = http_client.put(uri, body: format_body(body), header: request_headers({"Content-Type" => CONTENT_TYPE}.merge(header))) end
qualified_uri(server, path)
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 48 def qualified_uri(server, path) uri = test_env.servers[server.to_sym] raise "Test Error: #{server} doesn't appear to be defined in the environments.yml" if uri.nil? File.join(uri.to_s, path) end
query()
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 13 def query @query ||= {} end
request_headers(header = {})
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 17 def request_headers(header = {}) @request_headers ||= { "Accept" => CONTENT_TYPE } if @access_token @request_headers["Authorization"] = "Bearer #{@access_token}" else @request_headers.delete("Authorization") end @request_headers.merge!(header) @request_headers end
set_query_param(name, value)
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 30 def set_query_param(name, value) if value.respond_to?(:each) value.each { |v| set_query_param(name, v) } else name = name.camel_case value = is_enum_param(name) ? value.snake_case(:upper) : value current_value = query[name] if current_value.nil? query[name] = value elsif current_value.is_a?(Array) query[name] << value else query[name] = [current_value, value] end end end
Private Instance Methods
format_body(body)
click to toggle source
# File lib/cucumber/blinkbox/requests.rb, line 80 def format_body(body) body.is_a?(Hash) ? JSON.dump(body) : body end
is_enum_param(name)
click to toggle source
So that we don’t have to put enum parameters in the Gherkin in SCREAMING_SNAKE_CASE this heuristic identifies enum parameters so that we can transform them, meaning an enum value like PURCHASE_DATE can be written in the Gherkin as “purchase date” but still be sent correctly. This heuristic will need updating over time.
# File lib/cucumber/blinkbox/requests.rb, line 87 def is_enum_param(name) ["bookmarkType", "order", "role"].include?(name) end