Permalink
Browse files

general cleanup

git-svn-id: svn+ssh://rubyforge.org/var/svn/simplehttp/trunk@21 0af4886c-b8f6-4d8a-922d-87859854324b
  • Loading branch information...
1 parent a38e1ec commit e372f13b6e50f9826e17603b1bae5e0b5358ce87 a2800276 committed Sep 22, 2008
Showing with 734 additions and 700 deletions.
  1. +4 −0 CHANGELOG
  2. +447 −416 lib/simplehttp.rb
  3. +199 −200 test/http_test.rb
  4. +84 −84 test/http_test_server.rb
View
4 CHANGELOG
@@ -1,3 +1,7 @@
+= Mon Sep 22 15:12:44 CEST 2008
+* General code cleanup
+* Additional Docs
+* Reluctantly changes tabs to (ruby default) spaces (YUCK!)
= Mon Sep 22 13:25:22 CEST 2008
* bugfix: uri query parameter preserved on post req
View
863 lib/simplehttp.rb
@@ -24,429 +24,460 @@
# set.
# * SSL handled automatically
# * fault tolerant uri, e.g. all of these would work:
-# "www.example.com", "www.example.com/", "http://www.example.com"
+# "www.example.com", "www.example.com/", "http://www.example.com"
#
# Some usage examples:
-# # plain GET (using class methods)
-# SimpleHttp.get "www.example.com"
+# # plain GET (using class methods)
+# SimpleHttp.get "www.example.com"
#
-# # POST using the instance methods
-# uri = URI.parse "https://www.example.com/index.html"
-# sh = SimpleHttp uri
-# sh.set_proxy "my.proxy", "8080"
-# sh.post {"query" => "query_data"}
+# # POST using the instance methods
+# uri = URI.parse "https://www.example.com/index.html"
+# sh = SimpleHttp uri
+# sh.set_proxy "my.proxy", "8080"
+# sh.post {"query" => "query_data"}
#
-# # POST using class methods.
-# binaryData = getImage
-# SimpleData.post binaryData, "image/png"
+# # POST using class methods.
+# binaryData = getImage
+# SimpleData.post binaryData, "image/png"
#
-# # GET requst with a custom request_header
-# sh = SimpleHttp.new "http://www.example.com"
-# sh.request_headers= {'X-Special-Http-Header'=>'my-value'}
-# sh.get
+# # GET requst with a custom request_header
+# sh = SimpleHttp.new "http://www.example.com"
+# sh.request_headers= {'X-Special-Http-Header'=>'my-value'}
+# sh.get
class SimpleHttp
-
- VERSION='0.1.3'
-
- attr_accessor :proxy_host, :proxy_port, :proxy_user, :proxy_pwd, :uri, :request_headers, :response_headers, :response_handlers, :follow_num_redirects
- RESPONSE_HANDLERS = {
- Net::HTTPResponse => lambda { |request, response, http|
- response.each_header {|key, value|
- http.response_headers[key]=value
- }
- raise "#{response.to_s} : #{response.code} : #{http.uri}"
- },
- Net::HTTPSuccess => lambda { |request, response, http|
- response.each_header {|key, value|
- http.response_headers[key]=value
- }
- #http.cookies += response.cookies
- if request.class == Net::HTTP::Head || request.class == Net::HTTP::Options
- return http.response_headers
- end
- return response.body
- },
- Net::HTTPRedirection => lambda { |request, response, http|
- raise "too many redirects!" unless http.follow_num_redirects > 0
- # create a new SimpleHttp for the location
- # refered to decreasing the remaining redirects
- # by one.
-
- if (location = response['location']) !~ /^https?:\/\//
- new_location = "#{http.uri.scheme}://#{http.uri.host}"
- if location =~ /^\//
- new_location += location
- else
- new_location += "/#{http.uri.path}/#{location}"
- end
- location = new_location
- end
-
- sh = SimpleHttp.new location
- #STDERR.puts location
- sh.follow_num_redirects = http.follow_num_redirects-1
-
- # copy the response handlers used in the current
- # request in case they were non standard.
- sh.response_handlers = http.response_handlers
-
- # copy the request headers
- sh.request_headers=http.request_headers
- sh.response_headers=http.response_headers
- #sh.cookies+=http.cookies
-
- # copy host and port
- sh.uri.host = http.uri.host
- sh.uri.port = http.uri.port
-
- # HTTP doesn't permit redirects for methods other than
- # GET or HEAD. The exception is 303 redirects, which
- # should automatically follow the redirect URI using a
- # GET method regardless of the initial method. For
- # other classes of redirection, the client is required
- # to prompt the user before redirection occurs. Because
- # that's not a feasible action for this library, all
- # 3xx redirect URIs are followed using a GET method.
- #
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
-
- case request
- when Net::HTTP::Get,
- Net::HTTP::Head,
- Net::HTTP::Options
-
- sh.get
- when Net::HTTP::Post
- sh.request_headers['content-length']=nil
- sh.get
- else
- raise "Not a valid HTTP method for redirection: #{request.class}"
- end
-
- }
-
- }
-
- # SimpleHttp can either be used directly through the +get+ and
- # +post+ class methods or be instantiated, in case you need to
- # to add custom behaviour to the requests.
- #
- # @param may be a URI or a String.
- #
- # Example:
- # http = SimpleHttp.new(URI.parse("http://www.example.com"))
- # http = SimpleHttp.new "www.example.com"
- # http = SimpleHttp.new "http://usr:pwd@www.example.com:1234"
- def initialize uri
- set_proxy ENV['http_proxy'] if ENV['http_proxy']
-
- if uri.class == String
-
- unless uri =~ /^https?:\/\//
- uri = "http://#{uri}"
- end
-
- uri = URI.parse uri
-
- end
- @uri = uri
- if !@uri.path || "" == @uri.path.strip
- @uri.path="/"
- end
-
-
- @request_headers={}
- @response_headers={}
- @cookies=[]
- @response_handlers=RESPONSE_HANDLERS.clone
- @follow_num_redirects=5
-
- if @uri.user
- basic_authentication @uri.user, @uri.password
- end
-
- end
-
- #
- # Provides facilities to perform http basic authentication. You
- # don't need to provide +usr+ and +pwd+ if they are already included
- # in the uri, i.e. http://user:password@www.example.com/
- #
-
- def basic_authentication usr, pwd
- str = Base64.encode64("#{usr}:#{pwd}")
- str = "Basic #{str}"
- @request_headers["Authorization"]=str
- end
-
- #
- # this method can be used to register response handlers for specific
- # http responses in case you need to override the default behaviour.
- # Defaults are:
- #
- # HTTPSuccess : return the body of the response
- # HTTPRedirection : follow the redirection until success
- # Others : raise an exception
- #
- # `clazz` is the subclass of HTTPResponse (or HTTPResponse in case you
- # want to define "default" behaviour) that you are registering the
- # handler for.
- #
- # `block` is the handler itself, if a response of the appropriate class
- # is received, `block` is called with three parameters: the the
- # Net::HTTPRequest, the actual HTTPResponse object that was received
- # and a reference to the instance of `SimpleHttp` that is executing the
- # call.
- #
- # example:
- #
- # # to override the default action of following a HTTP
- # # redirect, you could register the folllowing handler:
- #
- # sh = SimpleHttp "www.example.com"
- # sh.register_response_handler Net::HTTPRedirection {|request, response, shttp|
- # response['location']
- # }
- #
-
- def register_response_handler clazz, &block
- c = clazz
- while c != Object
- # completely unnecessary sanity check to make sure parameter
- # `clazz` is in fact a HTTPResponse ...
- if c == Net::HTTPResponse
- @response_handlers[clazz]=block
- return
- end
- c = c.superclass
- end
-
- raise "Trying to register a response handler for non-response class: #{clazz}"
- end
-
- #
- # Set the proxy to use for the http request.
- # Note that you don't need to set the proxy in case the
- # `http_proxy` environment variable is set. To override
- # previous proxy settings and connect directly, call
- # `set_proxy nil`
- #
- # usage:
- # http = SimpleHttp.new "www.example.com"
- #
- # http.set_proxy "http://proxy:8000"
- # or:
- # http.set_proxy(URI.parse("http://proxy:8000"))
- # or:
- # http.set_proxy 'proxy', '8000', 'my_user', 'secret'
- # or:
- # http.set_proxy nil # to override previous proxy
- # settings and make the request directly.
- #
-
-
- def set_proxy proxy, port=nil, user=nil, pwd=nil
-
-
- if !proxy
- @proxy_host=@proxy_port=@proxy_user=@proxy_pwd=nil
- return
- end
-
- if proxy.class == String
- if !port && !user && !pwd
- proxy = URI.parse(proxy)
- else
- @proxy_host= host
- @proxy_port= port
- @proxy_user= user
- @proxy_pwd = pwd
- end
- end
-
- if proxy.class == URI::HTTP
- @proxy_host= proxy.host
- @proxy_port= proxy.port
- @proxy_user= proxy.user
- @proxy_pwd = proxy.password
- end
- end
-
- # interal
- # Takes a HTTPResponse (or subclass) and determines how to
- # handle the response. Default behaviour is:
- #
- # HTTPSuccess : return the body of the response
- # HTTPRedirection : follow the redirect until success.
- # default : raise the HTTPResponse.
- #
- # the default behaviour can be overidden by registering a
- # response handler using the `register_response_handler` method.
- #
-
- def handle_response http_request, http_response
- raise "Not a Net::HTTPResponse" unless http_response.is_a? Net::HTTPResponse
-
- c = http_response.class
- while c!=Object
- # the response_handlers hash contains a handler
- # for the specific response class.
- if @response_handlers[c]
- #STDERR.puts "!#{http_response.class}"
- #STDERR.puts "!#{@response_handlers[c]}"
- return @response_handlers[c].call(http_request, http_response, self)
- end
-
- c=c.superclass
- end
-
- # if we reached this place, no handler was registered
- # for this response. default is to return the response.
-
- return http_response
- end
-
- # internal
- def do_http request
- response = nil
-
- http = Net::HTTP.new(@uri.host, @uri.port, @proxy_host,
- @proxy_port, @proxy_user, @proxy_pwd)
- http.use_ssl = @uri.scheme == 'https'
-
- # add custom request headers.
-
- @request_headers.each {|key,value|
- request[key]=value;
- }
-
- handle_response(request, http.request(request));
- end
-
- # internal
- def make_query query
- return query unless query && query.class == Hash
- query.inject([]) do |s, (key, value)|
- s << CGI::escape(key) + "=" + CGI::escape(value)
- end.join('&')
- end
-
- # Make a simple GET request to the provided URI.
- #
- # Example:
- # puts(SimpleHttp.get("www.example.com"))
- def self.get uri, query=nil
- http = SimpleHttp.new uri
- http.get query
- end
-
- def self.head uri, query=nil
- http = SimpleHttp.new uri
- http.head query
- end
-
- def self.options uri
- http = SimpleHttp.new uri
- http.options
- end
-
- def self.trace uri
- http = SimpleHttp.new uri
- http.trace
- end
-
- # Make a POST request to the provided URI.
- #
- # Example:
- # puts(SimpleHttp.post("www.example.com", "query"=>"my_query"))
- #
- # Alternatively, you can post any sort of data, but will have to
- # set the appriate content_type:
- #
- # SimpleHttp.post("http://www.example.com/", binary_data, "img/png")
-
- def self.post uri, query=nil, content_type='application/x-www-form-urlencoded'
- http = SimpleHttp.new uri
- http.post query, content_type
- end
-
- # Call the +get+ method as an instance method if you need to
- # modify the default behaviour of the library, or set special
- # headers:
- #
- # http = SimpleHttp.new "www.example.com"
- # http.request_headers["X-Special"]="whatever"
- # str = http.get
- def get query = nil
- if (query = make_query query)
- @uri.query = @uri.query ? @uri.query+"&"+query : query
- end
- full_path = @uri.path + (@uri.query ? "?#{@uri.query}" : "")
-
- req = Net::HTTP::Get.new(full_path)
- # puts Net::HTTP::Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pwd).get(@uri)
- do_http req
- end
-
- # Call the +head+ method as an instance method.
- #
- def head query = nil
- if (query = make_query query)
- @uri.query = @uri.query ? @uri.query+"&"+query : query
- end
- full_path = @uri.path + (@uri.query ? "?#{@uri.query}" : "")
-
- req = Net::HTTP::Head.new(full_path)
- # puts Net::HTTP::Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pwd).get(@uri)
- do_http req
- end
-
- # Call http +options+ method. Returns the response
- def options
- # we don't support sending a payload in options' body.
- req = Net::HTTP::Options.new(@uri.path)
- do_http req
- end
-
- def trace
- # payload?
- req = Net::HTTP::Trace.new(@uri.path)
- do_http req
- end
-
- #
- # Post the query data to the url.
- # The body of the request remains empty if query=nil.
- # In case `query` is a `Hash`, it's assumed that we are
- # sending a form.
- # In case `query` is a `String`, it's also assumed that a
- # form is being sent, UNLESS the `content_type` parameter
- # is set.
- #
- def post query=nil, content_type='application/x-www-form-urlencoded'
- path = @uri.query ? "#{@uri.path}?#{@uri.query}" : @uri.path
- req = Net::HTTP::Post.new(path)
-
-
- req.body= make_query query if query
- req.content_type=content_type if query
- req.content_length=query ? req.body.length : 0
-
- do_http req
- end
-
+
+ VERSION='0.1.4'
+
+ # Host component of proxy uri
+ attr_accessor :proxy_host
+ # Port component of proxy uri
+ attr_accessor :proxy_port
+ # Proxy User
+ attr_accessor :proxy_user
+ # Proxy Password
+ attr_accessor :proxy_pwd
+ # The +URI+ object to connect to
+ attr_reader :uri
+ # +Hash+ of headers that will be sent in the request.
+ attr_accessor :request_headers
+ # +Hash+ of headers that were set in the response.
+ attr_accessor :response_headers
+ # A +Hash+ of handlers for each class of HTTPResponse.
+ attr_accessor :response_handlers
+ # The number of redirects we should follow. Default 5.
+ # An exception gets raised after the fifth redirect.
+ attr_accessor :follow_num_redirects
+
+ RESPONSE_HANDLERS = {
+ Net::HTTPResponse => lambda { |request, response, http|
+ http._update_response_headers(response)
+ raise "#{response.to_s} : #{response.code} : #{http.uri}"
+ },
+ Net::HTTPSuccess => lambda { |request, response, http|
+ http._update_response_headers(response)
+ #http.cookies += response.cookies
+ case request
+ when Net::HTTP::Head, Net::HTTP::Options
+ http.response_headers
+ else
+ response.body
+ end
+ },
+ Net::HTTPRedirection => lambda { |request, response, http|
+ raise "too many redirects!" unless http.follow_num_redirects > 0
+ # create a new SimpleHttp for the location
+ # refered to decreasing the remaining redirects
+ # by one.
+
+ if (location = response['location']) !~ /^https?:\/\//
+ new_location = "#{http.uri.scheme}://#{http.uri.host}"
+ if location =~ /^\//
+ new_location += location
+ else
+ new_location += "/#{http.uri.path}/#{location}"
+ end
+ location = new_location
+ end
+
+ sh = SimpleHttp.new location
+ #STDERR.puts location
+ sh.follow_num_redirects = http.follow_num_redirects-1
+
+ # copy the response handlers used in the current
+ # request in case they were non standard.
+ sh.response_handlers = http.response_handlers
+
+ # copy the request headers
+ sh.request_headers=http.request_headers
+ sh.response_headers=http.response_headers
+ #sh.cookies+=http.cookies
+
+ # copy host and port
+ sh.uri.host = http.uri.host
+ sh.uri.port = http.uri.port
+
+ # HTTP doesn't permit redirects for methods other than
+ # GET or HEAD. The exception is 303 redirects, which
+ # should automatically follow the redirect URI using a
+ # GET method regardless of the initial method. For
+ # other classes of redirection, the client is required
+ # to prompt the user before redirection occurs. Because
+ # that's not a feasible action for this library, all
+ # 3xx redirect URIs are followed using a GET method.
+ #
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
+
+ case request
+ when Net::HTTP::Get,
+ Net::HTTP::Head,
+ Net::HTTP::Options
+ sh.get
+ when Net::HTTP::Post
+ sh.request_headers['content-length']=nil
+ sh.get
+ else
+ raise "Not a valid HTTP method for redirection: #{request.class}"
+ end
+
+ }
+
+ }
+
+ # SimpleHttp can either be used directly through the +get+ and
+ # +post+ class methods or be instantiated, in case you need to
+ # to add custom behaviour to the requests.
+ #
+ # @param may be a URI or a String.
+ #
+ # Example:
+ # http = SimpleHttp.new(URI.parse("http://www.example.com"))
+ # http = SimpleHttp.new "www.example.com"
+ # http = SimpleHttp.new "http://usr:pwd@www.example.com:1234"
+ def initialize uri
+ set_proxy ENV['http_proxy'] if ENV['http_proxy']
+
+ if uri.class == String
+ unless uri =~ /^https?:\/\//
+ uri = "http://#{uri}"
+ end
+ uri = URI.parse uri
+ end
+ @uri = uri
+
+ if !@uri.path || "" == @uri.path.strip
+ @uri.path="/"
+ end
+
+ @request_headers={}
+ @response_headers={}
+ @cookies=[]
+ @response_handlers=RESPONSE_HANDLERS.clone
+ @follow_num_redirects=5
+
+ if @uri.user
+ basic_authentication @uri.user, @uri.password
+ end
+
+ end
+
+ #
+ # Provides facilities to perform http basic authentication. You
+ # don't need to provide +usr+ and +pwd+ if they are already included
+ # in the uri, i.e. http://user:password@www.example.com/
+ #
+ # Usage:
+ # sh = SimpleHttp.new "www.example.com/password_protected_resource"
+ # sh.basic_authentication "user_name", "secret_password"
+ # sh.get
+ #
+
+ def basic_authentication usr, pwd
+ str = Base64.encode64("#{usr}:#{pwd}")
+ str = "Basic #{str}"
+ @request_headers["Authorization"]=str
+ end
+
+ #
+ # This method can be used to register response handlers for specific
+ # http responses in case you need to override the default behaviour.
+ # Defaults are:
+ #
+ # <b>HTTPSuccess (200-206)</b> :: return the body of the response
+ # <b>HTTPRedirection (300-307)</b> :: follow the redirection until success
+ # *Others* :: raise an exception
+ #
+ # ===Parameters:
+ #
+ # +clazz+ is the subclass of <code>Net::HTTPResponse</code> (or +HTTPResponse+ in case you
+ # want to define "default" behaviour) that you are registering the
+ # handler for. E.g. to register a handler for a HTTP 303 response, +clazz+
+ # needs to be +HTTPSeeOther+.
+ #
+ # +block+ is the handler itself. When a response of the appropriate class
+ # is received by the library, +block+ is called with three parameters: the
+ # 'raw' <code>Net::HTTPRequest</code>, the actual +HTTPResponse+ object that was received
+ # and a reference to the instance of +SimpleHttp+ that is executing the
+ # call.
+ #
+ # ===Example:
+ #
+ # # to override the default action of following a HTTP
+ # # redirect, you could register the folllowing handler:
+ #
+ # sh = SimpleHttp "www.example.com"
+ # sh.register_response_handler Net::HTTPRedirection {|request, response, shttp|
+ # response['location']
+ # }
+ #
+
+ def register_response_handler clazz, &block
+ # completely unnecessary sanity check to make sure parameter
+ # `clazz` is in fact a HTTPResponse ...
+ unless clazz.ancestors.include? Net::HTTPResponse
+ raise "Trying to register a response handler for non-response class: #{clazz}"
+ end
+ @response_handlers[clazz]=block
+
+ end
+
+ #
+ # Set the proxy to use for the http request.
+ #
+ # Note: you don't need to set the proxy in case the
+ # +http_proxy+ environment variable is set.
+ #
+ # To override previous proxy settings and connect directly,
+ # call +set_proxy+ +nil+.
+ #
+ # ===Usage:
+ #
+ # http = SimpleHttp.new "www.example.com"
+ #
+ # http.set_proxy "http://proxy:8000"
+ # or:
+ # http.set_proxy(URI.parse("http://proxy:8000"))
+ # or:
+ # http.set_proxy 'proxy', '8000', 'my_user', 'secret'
+ # or:
+ # http.set_proxy nil # to override previous proxy
+ # settings and make the request directly.
+ #
+
+
+ def set_proxy proxy, port=nil, user=nil, pwd=nil
+
+
+ if !proxy
+ @proxy_host=@proxy_port=@proxy_user=@proxy_pwd=nil
+ return
+ end
+
+ if String === proxy
+ if !port && !user && !pwd
+ proxy = URI.parse(proxy)
+ else
+ @proxy_host= host
+ @proxy_port= port
+ @proxy_user= user
+ @proxy_pwd = pwd
+ end
+ end
+
+ if URI::HTTP === proxy
+ @proxy_host= proxy.host
+ @proxy_port= proxy.port
+ @proxy_user= proxy.user
+ @proxy_pwd = proxy.password
+ end
+ end
+
+ # ===Internal
+ # Takes a HTTPResponse (or subclass) and determines how to
+ # handle the response. Default behaviour is:
+ #
+ # HTTPSuccess : return the body of the response
+ # HTTPRedirection : follow the redirect until success.
+ # default : raise the HTTPResponse.
+ #
+ # the default behaviour can be overidden by registering a
+ # response handler using the `register_response_handler` method.
+ #
+
+ def _handle_response http_request, http_response
+ raise "Not a Net::HTTPResponse" unless http_response.is_a? Net::HTTPResponse
+
+ c = http_response.class
+ # Go up the inheritance chain to find the most specific handler
+ # for the class of response we received.
+ while c!=Object
+ # the response_handlers hash contains a handler
+ # for the specific response class.
+ if @response_handlers[c]
+ return @response_handlers[c].call(http_request, http_response, self)
+ end
+ c=c.superclass
+ end
+
+ # if we reached this place, no handler was registered
+ # for this response. default is to return the response.
+
+ return http_response
+ end
+
+ # ===Internal
+ #
+ # ===Parameter
+ # +request+ the <code>Net::HTTPRequest</code> to process.
+ def _do_http request
+ http = Net::HTTP.new(@uri.host, @uri.port,
+ @proxy_host, @proxy_port, @proxy_user, @proxy_pwd)
+ http.use_ssl = @uri.scheme == 'https'
+
+ # add custom request headers.
+ @request_headers.each {|key,value|
+ request[key]=value;
+ }
+
+ response = http.request(request)
+ _handle_response(request, response);
+ end
+
+ # ===Internal
+ def _make_query query
+ return query unless query && query.class == Hash
+ query.inject([]) do |s, (key, value)|
+ s << CGI::escape(key) + "=" + CGI::escape(value)
+ end.join('&')
+ end
+
+ # Make a simple GET request to the provided URI.
+ #
+ # ===Parameter
+ #
+ # +uri+ :: the uri to connect to, may be a +URI+ or a +String+
+ # +query+ :: the query part of the +get+, may be a +String+ or +Hash+
+ #
+ # ===Usage:
+ #
+ # puts(SimpleHttp.get("www.example.com"))
+ # puts(SimpleHttp.get("www.example.com", "param"=>"value")
+ def self.get uri, query=nil
+ http = SimpleHttp.new uri
+ http.get query
+ end
+
+ # Make a simple +HEAD+ request
+ #
+ # ===Parameter
+ # see +get+
+ def self.head uri, query=nil
+ http = SimpleHttp.new uri
+ http.head query
+ end
+
+ # Make a simple +OPTIONS+ request
+ def self.options uri
+ http = SimpleHttp.new uri
+ http.options
+ end
+
+ # Make a simple +TRACE+ request
+ def self.trace uri
+ http = SimpleHttp.new uri
+ http.trace
+ end
+
+ # Make a POST request to the provided URI.
+ #
+ # ===Example:
+ # puts(SimpleHttp.post("www.example.com", "query"=>"my_query"))
+ #
+ # Alternatively, to post arbitrary data, all you need to do is
+ # set the appriate +content_type+:
+ #
+ # SimpleHttp.post("http://www.example.com/", binary_data, "img/png")
+
+ def self.post uri, query=nil, content_type='application/x-www-form-urlencoded'
+ http = SimpleHttp.new uri
+ http.post query, content_type
+ end
+
+ # Call the +get+ method as an instance method if you need to
+ # modify the default behaviour of the library, or set special
+ # headers:
+ #
+ # http = SimpleHttp.new "www.example.com"
+ # http.request_headers["X-Special"]="whatever"
+ # str = http.get
+ def get query = nil
+ req = Net::HTTP::Get.new( _handle_path(query) )
+ # puts Net::HTTP::Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pwd).get(@uri)
+ _do_http req
+ end
+
+ #
+ # Call the +head+ method as an instance method.
+ # see +head+
+ def head query = nil
+ req = Net::HTTP::Head.new( _handle_path(query) )
+ # puts Net::HTTP::Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pwd).get(@uri)
+ _do_http req
+ end
+
+ # Call http +options+ method. Returns the response
+ # see +options+
+ def options
+ # we don't support sending a payload in options' body.
+ req = Net::HTTP::Options.new(@uri.path)
+ _do_http req
+ end
+
+ # Call http +trace+ method. Returns the response
+ # see +trace+
+ def trace
+ # payload?
+ req = Net::HTTP::Trace.new(@uri.path)
+ _do_http req
+ end
+
+ #
+ # Post the query data to the url.
+ #
+ # The body of the request remains empty if query=nil.
+ #
+ # In case +query+ is a +Hash+, it's assumed that we are
+ # sending a form.
+ #
+ # In case +query+ is a +String+, it's also assumed that a
+ # form is being sent, UNLESS the +content_type+ parameter
+ # is set.
+ #
+ def post query=nil, content_type='application/x-www-form-urlencoded'
+ req = Net::HTTP::Post.new( _handle_path() )
+ req.body= _make_query query if query
+ req.content_type=content_type if query
+ req.content_length=query ? req.body.length : 0
+
+ _do_http req
+ end
+
+ def _handle_path query=nil
+ if (query = _make_query query)
+ @uri.query = @uri.query ? @uri.query+"&"+query : query
+ end
+ path = @uri.query ? "#{uri.path}?#{@uri.query}" : @uri.path
+ end
+
+ # ===Internal
+ # Used in the response handler to set the value of the
+ # response header fields.
+ def _update_response_headers http_response
+ http_response.each_header {|key, value|
+ self.response_headers[key]=value
+ }
+ end
+
end
-#ht = SimpleHttp.new "http://www.google.com/aldfksjaldskjfalskjfdlk"
-##ht.register_response_handler(Net::HTTPRedirection) {|req, res, ht| puts res['location']}
-#puts ht.get.class
-##puts(ht.get("q"=>"bla"))
-#
-##puts (SimpleHttp.get "http://www.google.com")
-#
-#['http://www.google.com/', 'www.google.com', 'https://www.google.com'].each {|u|
-# SimpleHttp.new u
-#}
-##puts ht.post
View
399 test/http_test.rb
@@ -9,206 +9,205 @@
# implemented in `http_test_server.rb`
class SimpleHttpTest < Test::Unit::TestCase
- def initialize *args
- super *args
- # create webbrick server in a separate thread
- Thread.new {
- TestServer.new.start
- }
- end
-
- def teardown
- #@t.shutdown # webrick blocks in a weird way, can't
- #commnicate with it in a different thread. this, of
- #course, is really ugly.
- end
-
- # Tests of SimpleHttp class method behaviour.
-
- def test_get_class_method
- # string url
- ret = SimpleHttp.get "http://127.0.0.1:12345/test1"
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 1 test failed.");
-
- # string url, with query
- ret = SimpleHttp.get "http://127.0.0.1:12345/test1?query=true"
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 2 test failed.");
-
- # uri
- uri = URI.parse "http://127.0.0.1:12345/test1?query=true"
- ret = SimpleHttp.get uri
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 3 test failed.");
-
- #string with query as hash
- ret = SimpleHttp.get "http://127.0.0.1:12345/test2", "query" => "query_test"
- assert_equal("query_test", ret, "basic GET (query) 4 test failed.");
-
- #uri with existing query + query as hash.
- uri = URI.parse "http://127.0.0.1:12345/test2?query2=true"
- ret = SimpleHttp.get uri, "query" => "query_test"
- assert_equal("query_test", ret, "basic GET (query) 4.1 test failed.");
-
-
- end
-
- # Tests for http GET calls implemented using instantiated SimpleHttp objects.
-
- def test_get_class_instance_method
- http = SimpleHttp.new "http://127.0.0.1:12345/test1"
- ret = http.get
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "instance GET 1 test failed.");
-
- http = SimpleHttp.new "http://127.0.0.1:12345/test2?query=query_test"
- ret = http.get
- assert_equal("query_test", ret, "instance GET 2 test (query)");
-
- http = SimpleHttp.new "http://127.0.0.1:12345/query_test2?query=query_test"
- ret = http.get "query2"=>"query2_test"
- assert_equal("query2_test", ret, "instance GET 2.1 test (add to existing query")
-
- http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
- ret = http.get "query2" => "query_test"
- assert_equal("query_test", ret, "basic GET (query) 3 test failed.")
-
- http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
- ret = http.get "something"=>"or the other", "query2" => "query_test"
- assert_equal("query_test", ret, "basic GET (query) 4 test failed.")
-
- # GET request with a custom request_header
- http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
- http.request_headers= {'x-special-http-header'=>'my-value'}
- ret = http.get
- assert_equal("my-value", ret, "GET header test 4")
-
- # GET test with custom response_headers
- http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
- http.request_headers= {'x-special-http-header'=>'resp-header-test'}
- http.get
- assert_equal("resp-header-test", http.response_headers['x-special-response'], "GET response header test 5")
-
- # GET test with custom request & response header and redirect
- # test that request headers we set remain intact during redirects.
-
- http = SimpleHttp.new "http://127.0.0.1:12345/redirect_special_header"
- http.request_headers["x-special-http-header"]='redirect_test'
- http.get
- assert_equal("redirect_test", http.response_headers['x-special-response'], "GET response header redirect test. 6")
-
- end
-
- # http POST calls using class methods.
- #
- def test_post_class_method
-
- # string url
- ret = SimpleHttp.post "http://127.0.0.1:12345/test1"
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic POST 1 test failed.");
-
-
- #string with query as hash
- ret = SimpleHttp.post "http://127.0.0.1:12345/test2", "query" => "query_test"
- assert_equal("query_test", ret, "basic POST (query) 2 test failed.");
-
- #uri with existing query + query as hash.
- uri = URI.parse "http://127.0.0.1:12345/test2"
- ret = SimpleHttp.post uri, "query" => "query_test"
- assert_equal("query_test", ret, "basic POST (query) 2.1 test failed.");
-
- # post something other than a form.
- ret = SimpleHttp.post "http://127.0.0.1:12345/post_data_test", TestServer::POST_DATA, "misc/test-data"
- assert_equal(TestServer::POST_DATA, ret, "class Post data test")
-
-
- end
-
-
- def test_post_instance_method
- http = SimpleHttp.new "http://127.0.0.1:12345/test1"
- ret = http.post
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "instance POST 1 test failed.");
-
- http = SimpleHttp.new "http://127.0.0.1:12345/test2"
- ret = http.post "query" => "query_test"
- assert_equal("query_test", ret, "instance POST 2 test (query)");
-
- http = SimpleHttp.new "http://127.0.0.1:12345/query_test2?query=query_test"
- ret = http.post "query2"=>"query2_test"
- assert_equal("query2_test", ret, "instance POST 2.1 test (add to existing query")
-
- http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
- ret = http.post "query2" => "query_test"
- assert_equal("query_test", ret, "basic POST (query) 3 test failed.")
-
- http = SimpleHttp.new "http://127.0.0.1:12345/post_test_uri_query?query=query_test"
- ret = http.post "query2"=>"query2_test"
- assert_equal("query=query_test", ret, "instance POST 3.1 test (query on url for post query.)")
-
- # POST requst with a custom request_header
- http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
- http.request_headers= {'x-special-http-header'=>'my-value'}
- ret = http.post
- assert_equal("my-value", ret, "POST header test 4")
-
- # POST request with a custom data type (not posting a form.)
- http = SimpleHttp.new "http://127.0.0.1:12345/post_data_test"
- ret = http.post TestServer::POST_DATA, "misc/test-data"
- assert_equal(TestServer::POST_DATA, ret, "Post data test")
-
-
- end
-
- def test_basic_auth
-
- # string url
- ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/basic_auth"
- assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth get class 1 test failed.");
-
- http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
- http.basic_authentication "test", "pwd"
- ret = http.get
- assert_equal(ret, TestServer::SUCCESS_TEXT_1, "basic auth get instance 2 test failed.");
- #string with query as hash
- ret = SimpleHttp.post "http://test:pwd@127.0.0.1:12345/basic_auth", "query" => "query_test"
- assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth post class 3 test failed.");
-
- http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
- http.basic_authentication "test", "pwd"
- ret = http.post TestServer::POST_DATA, "misc/test-data"
- assert_equal(TestServer::SUCCESS_TEXT_1, ret, "Post data test")
- end
-
- def test_response_handler
- assert_raise(RuntimeError) {
- ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/non_existant"
- }
-
- http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/non_existant"
- http.register_response_handler(Net::HTTPNotFound) { |req, res, http|
- res.code
- }
- ret = http.get
- assert_equal("404", ret, "response handler test 2")
-
- http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
- http.register_response_handler(Net::HTTPRedirection) { |req, res, http|
- res['location']
- }
- ret = http.get
- assert_equal("http://127.0.0.1:12345/redirect2", ret, "response handler test 3")
-
- end
-
- def test_redirect
- assert_raise(RuntimeError) {
- http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
- ret = http.get
- }
-
-
- ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/redirect"
- assert_equal(ret, TestServer::SUCCESS_TEXT_0, "redirect test 2");
- end
-
+ def initialize *args
+ super *args
+ # create webbrick server in a separate thread
+ Thread.new {
+ TestServer.new.start
+ }
+ end
+
+ def teardown
+ #@t.shutdown # webrick blocks in a weird way, can't
+ #commnicate with it in a different thread. this, of
+ #course, is really ugly.
+ end
+
+ # Tests of SimpleHttp class method behaviour.
+
+ def test_get_class_method
+ # string url
+ ret = SimpleHttp.get "http://127.0.0.1:12345/test1"
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 1 test failed.");
+
+ # string url, with query
+ ret = SimpleHttp.get "http://127.0.0.1:12345/test1?query=true"
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 2 test failed.");
+
+ # uri
+ uri = URI.parse "http://127.0.0.1:12345/test1?query=true"
+ ret = SimpleHttp.get uri
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 3 test failed.");
+
+ #string with query as hash
+ ret = SimpleHttp.get "http://127.0.0.1:12345/test2", "query" => "query_test"
+ assert_equal("query_test", ret, "basic GET (query) 4 test failed.");
+
+ #uri with existing query + query as hash.
+ uri = URI.parse "http://127.0.0.1:12345/test2?query2=true"
+ ret = SimpleHttp.get uri, "query" => "query_test"
+ assert_equal("query_test", ret, "basic GET (query) 4.1 test failed.");
+
+
+ end
+
+ # Tests for http GET calls implemented using instantiated SimpleHttp objects.
+
+ def test_get_class_instance_method
+ http = SimpleHttp.new "http://127.0.0.1:12345/test1"
+ ret = http.get
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "instance GET 1 test failed.");
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/test2?query=query_test"
+ ret = http.get
+ assert_equal("query_test", ret, "instance GET 2 test (query)");
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/query_test2?query=query_test"
+ ret = http.get "query2"=>"query2_test"
+ assert_equal("query2_test", ret, "instance GET 2.1 test (add to existing query")
+
+ http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
+ ret = http.get "query2" => "query_test"
+ assert_equal("query_test", ret, "basic GET (query) 3 test failed.")
+
+ http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
+ ret = http.get "something"=>"or the other", "query2" => "query_test"
+ assert_equal("query_test", ret, "basic GET (query) 4 test failed.")
+
+ # GET request with a custom request_header
+ http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
+ http.request_headers= {'x-special-http-header'=>'my-value'}
+ ret = http.get
+ assert_equal("my-value", ret, "GET header test 4")
+
+ # GET test with custom response_headers
+ http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
+ http.request_headers= {'x-special-http-header'=>'resp-header-test'}
+ http.get
+ assert_equal("resp-header-test", http.response_headers['x-special-response'], "GET response header test 5")
+
+ # GET test with custom request & response header and redirect
+ # test that request headers we set remain intact during redirects.
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/redirect_special_header"
+ http.request_headers["x-special-http-header"]='redirect_test'
+ http.get
+ assert_equal("redirect_test", http.response_headers['x-special-response'], "GET response header redirect test. 6")
+
+ end
+
+ # http POST calls using class methods.
+ #
+ def test_post_class_method
+
+ # string url
+ ret = SimpleHttp.post "http://127.0.0.1:12345/test1"
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic POST 1 test failed.");
+
+
+ #string with query as hash
+ ret = SimpleHttp.post "http://127.0.0.1:12345/test2", "query" => "query_test"
+ assert_equal("query_test", ret, "basic POST (query) 2 test failed.");
+
+ #uri with existing query + query as hash.
+ uri = URI.parse "http://127.0.0.1:12345/test2"
+ ret = SimpleHttp.post uri, "query" => "query_test"
+ assert_equal("query_test", ret, "basic POST (query) 2.1 test failed.");
+
+ # post something other than a form.
+ ret = SimpleHttp.post "http://127.0.0.1:12345/post_data_test", TestServer::POST_DATA, "misc/test-data"
+ assert_equal(TestServer::POST_DATA, ret, "class Post data test")
+
+
+ end
+
+
+ def test_post_instance_method
+ http = SimpleHttp.new "http://127.0.0.1:12345/test1"
+ ret = http.post
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "instance POST 1 test failed.");
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/test2"
+ ret = http.post "query" => "query_test"
+ assert_equal("query_test", ret, "instance POST 2 test (query)");
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/query_test2?query=query_test"
+ ret = http.post "query2"=>"query2_test"
+ assert_equal("query2_test", ret, "instance POST 2.1 test (add to existing query")
+
+ http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
+ ret = http.post "query2" => "query_test"
+ assert_equal("query_test", ret, "basic POST (query) 3 test failed.")
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/post_test_uri_query?query=query_test"
+ ret = http.post "query2"=>"query2_test"
+ assert_equal("query=query_test", ret, "instance POST 3.1 test (query on url for post query.)")
+
+ # POST requst with a custom request_header
+ http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
+ http.request_headers= {'x-special-http-header'=>'my-value'}
+ ret = http.post
+ assert_equal("my-value", ret, "POST header test 4")
+
+ # POST request with a custom data type (not posting a form.)
+ http = SimpleHttp.new "http://127.0.0.1:12345/post_data_test"
+ ret = http.post TestServer::POST_DATA, "misc/test-data"
+ assert_equal(TestServer::POST_DATA, ret, "Post data test")
+
+
+ end
+
+ def test_basic_auth
+
+ # string url
+ ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/basic_auth"
+ assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth get class 1 test failed.");
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
+ http.basic_authentication "test", "pwd"
+ ret = http.get
+ assert_equal(ret, TestServer::SUCCESS_TEXT_1, "basic auth get instance 2 test failed.");
+ #string with query as hash
+ ret = SimpleHttp.post "http://test:pwd@127.0.0.1:12345/basic_auth", "query" => "query_test"
+ assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth post class 3 test failed.");
+
+ http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
+ http.basic_authentication "test", "pwd"
+ ret = http.post TestServer::POST_DATA, "misc/test-data"
+ assert_equal(TestServer::SUCCESS_TEXT_1, ret, "Post data test")
+ end
+
+ def test_response_handler
+ assert_raise(RuntimeError) {
+ ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/non_existant"
+ }
+
+ http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/non_existant"
+ http.register_response_handler(Net::HTTPNotFound) { |req, res, http|
+ res.code
+ }
+ ret = http.get
+ assert_equal("404", ret, "response handler test 2")
+
+ http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
+ http.register_response_handler(Net::HTTPRedirection) { |req, res, http|
+ res['location']
+ }
+ ret = http.get
+ assert_equal("http://127.0.0.1:12345/redirect2", ret, "response handler test 3")
+
+ end
+
+ def test_redirect
+ assert_raise(RuntimeError) {
+ http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
+ ret = http.get
+ }
+
+ ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/redirect"
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "redirect test 2");
+ end
+
end
View
168 test/http_test_server.rb
@@ -3,100 +3,100 @@
include WEBrick
class TestServer
- SUCCESS_TEXT_0 = "success_0"
- SUCCESS_TEXT_1 = "success_1"
- SUCCESS_TEXT_2 = "success_2"
- SUCCESS_TEXT_3 = "success_3"
- SUCCESS_TEXT_4 = "success_4"
- SUCCESS_TEXT_5 = "success_5"
- SUCCESS_TEXT_6 = "success_6"
- SUCCESS_TEXT_7 = "success_7"
- SUCCESS_TEXT_8 = "success_8"
- SUCCESS_TEXT_9 = "success_9"
+ SUCCESS_TEXT_0 = "success_0"
+ SUCCESS_TEXT_1 = "success_1"
+ SUCCESS_TEXT_2 = "success_2"
+ SUCCESS_TEXT_3 = "success_3"
+ SUCCESS_TEXT_4 = "success_4"
+ SUCCESS_TEXT_5 = "success_5"
+ SUCCESS_TEXT_6 = "success_6"
+ SUCCESS_TEXT_7 = "success_7"
+ SUCCESS_TEXT_8 = "success_8"
+ SUCCESS_TEXT_9 = "success_9"
- POST_DATA = [0xbe, 0x00, 0x12, 0x23, 0x45, 0x67, 0x89, 0xAB].join
+ POST_DATA = [0xbe, 0x00, 0x12, 0x23, 0x45, 0x67, 0x89, 0xAB].join
- def initialize
- @server = HTTPServer.new(
- :Port => 12345
- )
+ def initialize
+ @server = HTTPServer.new(
+ :Port => 12345
+ )
- add_tests
- end
-
- def start
- @server.start
- end
+ add_tests
+ end
- def shutdown
- @server.shutdown
- end
-
- def dbg str
- puts "!!!!!!!!! #{str}"
- end
- def add_tests
- # return a text
- @server.mount_proc("/test1"){|req, res|
- res.body=SUCCESS_TEXT_0
- }
- #return value of query param named "query"
- @server.mount_proc("/test2"){|req, res|
- res.body=req.query["query"]
- }
-
- # return value of query param named "query2"
- @server.mount_proc("/query_test2"){|req, res|
- res.body=req.query["query2"]
- }
-
- # return value of request header named "X-Special-Http-Header"
- @server.mount_proc("/header_test"){|req, res|
- res.body=req.header["x-special-http-header"][0]
- res['x-special-response']=req.header["x-special-http-header"][0]
- }
-
- # return value of request header named "X-Special-Http-Header"
- @server.mount_proc("/post_data_test"){|req, res|
- ctype = req.header['content-type'][0]
- if "misc/test-data" == ctype
- res.body=req.body
- else
- res.body="failed, content-type: #{ctype}"
- end
- }
+ def start
+ @server.start
+ end
+
+ def shutdown
+ @server.shutdown
+ end
+
+ def dbg str
+ puts "!!!!!!!!! #{str}"
+ end
+ def add_tests
+ # return a text
+ @server.mount_proc("/test1"){|req, res|
+ res.body=SUCCESS_TEXT_0
+ }
+ #return value of query param named "query"
+ @server.mount_proc("/test2"){|req, res|
+ res.body=req.query["query"]
+ }
+
+ # return value of query param named "query2"
+ @server.mount_proc("/query_test2"){|req, res|
+ res.body=req.query["query2"]
+ }
+
+ # return value of request header named "X-Special-Http-Header"
+ @server.mount_proc("/header_test"){|req, res|
+ res.body=req.header["x-special-http-header"][0]
+ res['x-special-response']=req.header["x-special-http-header"][0]
+ }
+
+ # return value of request header named "X-Special-Http-Header"
+ @server.mount_proc("/post_data_test"){|req, res|
+ ctype = req.header['content-type'][0]
+ if "misc/test-data" == ctype
+ res.body=req.body
+ else
+ res.body="failed, content-type: #{ctype}"
+ end
+ }
# Test that query in uri gets transmitted in post reqs as well.
@server.mount_proc("/post_test_uri_query") { |req, res|
res.body = req.query_string
}
- @server.mount_proc("/basic_auth") {|req, res|
-
- auth = Base64.decode64(req.header['authorization'][0].split[1])
- usr, pwd = auth.split(':')
- if ('test'==usr && 'pwd'==pwd)
- res.body=SUCCESS_TEXT_1
- else
- res.status=403
- end
- }
+ @server.mount_proc("/basic_auth") {|req, res|
+
+ auth = Base64.decode64(req.header['authorization'][0].split[1])
+ usr, pwd = auth.split(':')
+ if ('test'==usr && 'pwd'==pwd)
+ res.body=SUCCESS_TEXT_1
+ else
+ res.status=403
+ end
+ }
- 1.upto(6) {|i|
- @server.mount_proc("/redirect#{i}") { |req, res|
- res.status=301
- res.header['location']="http://127.0.0.1:12345/redirect#{i+1}"
- }
- }
-
- @server.mount_proc("/redirect") { |req, res|
- res.status=301
- res.header['location']="http://127.0.0.1:12345/test1"
- }
+ 1.upto(6) {|i|
+ @server.mount_proc("/redirect#{i}") { |req, res|
+ res.status=301
+ res.header['location']="http://127.0.0.1:12345/redirect#{i+1}"
+ }
+ }
- @server.mount_proc("/redirect_special_header") { |req, res|
- res.status=301
- res.header['location']="http://127.0.0.1:12345/header_test"
- }
- end
+ @server.mount_proc("/redirect") { |req, res|
+ res.status=301
+ res.header['location']="http://127.0.0.1:12345/test1"
+ }
+
+ @server.mount_proc("/redirect_special_header") { |req, res|
+ res.status=301
+ res.header['location']="http://127.0.0.1:12345/header_test"
+ }
+ end
end

0 comments on commit e372f13

Please sign in to comment.