Skip to content

Commit

Permalink
Added query params change.
Browse files Browse the repository at this point in the history
  • Loading branch information
NOX73 committed Dec 31, 2012
1 parent 6359c02 commit d7b6de4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions celluloid-http.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency('rack')
gem.add_runtime_dependency('celluloid-io')
gem.add_runtime_dependency('http_parser.rb')
gem.add_runtime_dependency('activesupport')

gem.add_development_dependency('turn')
gem.add_development_dependency('minitest')
Expand Down
30 changes: 25 additions & 5 deletions lib/celluloid-http/request.rb
@@ -1,22 +1,42 @@
require 'active_support/core_ext/object/to_query'

class Celluloid::Http::Request
extend Forwardable
DEFAULT_METHOD = :get
DEFAULT_HTTP_VERSION = '1.1'

attr_reader :url
attr_accessor :method
attr_accessor :method, :body

def_delegators :@uri, :scheme, :host, :port, :path, :query
def_delegators :@uri, :scheme, :host, :path, :port, :query

def initialize(url, options = {})
@url = url
@uri = URI.parse url
@method = options[:method] || DEFAULT_METHOD
@body = options[:body]

merge_query_params(options[:query_params]) if options[:query_params]
end

def query_params
@query_params ||= Rack::Utils.parse_nested_query @uri.query
end

def to_s
path = self.path.length.zero? ? "/" : self.path
"#{method.to_s.upcase} #{path} HTTP/#{DEFAULT_HTTP_VERSION}\nHOST: #{host}\n\n"
"#{method.to_s.upcase} #{url} HTTP/#{DEFAULT_HTTP_VERSION}\nHOST: #{host}\n\n"
end

def url
@uri.to_s
end

def query=(val)
@uri.query = val.is_a?(Hash) ? val.to_query : val
end

def merge_query_params(params)
query_params.merge! params
self.query = query_params
end

end
30 changes: 30 additions & 0 deletions test/unit/request_test.rb
Expand Up @@ -16,4 +16,34 @@ def test_create_http_request
assert_equal "param=value", request.query
end

def test_parse_query_params
request = Celluloid::Http::Request.new 'http://127.0.0.1?param1=value1&param2=value2'

assert_equal 'value1', request.query_params['param1']
assert_equal 'value2', request.query_params['param2']
end

def test_merge_query_params
request = Celluloid::Http::Request.new 'http://127.0.0.1?param1=value1&param2=value2', {
query_params: {'param3' => 'value3'}
}

assert_equal 'value1', request.query_params['param1']
assert_equal 'value2', request.query_params['param2']
assert_equal 'value3', request.query_params['param3']

assert_equal 'http://127.0.0.1?param1=value1&param2=value2&param3=value3', request.url
end

def test_change_query_params
request = Celluloid::Http::Request.new 'http://127.0.0.1?param1=value1&param2=value2'

request.query = request.query_params.merge 'param3' => 'value3'

assert_equal 'value1', request.query_params['param1']
assert_equal 'value2', request.query_params['param2']
assert_equal 'value3', request.query_params['param3']
end


end

0 comments on commit d7b6de4

Please sign in to comment.