File: server.rb

package info (click to toggle)
ruby-typhoeus 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: ruby: 4,381; makefile: 6
file content (116 lines) | stat: -rw-r--r-- 2,955 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
61
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env ruby
require 'json'
require 'zlib'
require 'sinatra/base'
require 'rack/typhoeus'

TESTSERVER = Sinatra.new do
  set :logging, false
  use Rack::Typhoeus::Middleware::ParamsDecoder

  fail_count = 0

  post '/file' do
    {
      'content-type' => params[:file][:type],
      'filename' => params[:file][:filename],
      'content' => params[:file][:tempfile].read,
      'request-content-type' => request.env['CONTENT_TYPE']
    }.to_json
  end

  get '/multiple-headers' do
    [200, { 'Set-Cookie' => %w[ foo bar ], 'Content-Type' => 'text/plain' }, ['']]
  end

  get '/cookies-test' do
    [200, { 'Set-Cookie' => %w(foo=bar bar=foo), 'Content-Type' => 'text/plain' }, ['']]
  end

  get '/cookies-test2' do
    [200, { 'Set-Cookie' => %w(foo2=bar bar2=foo), 'Content-Type' => 'text/plain' }, ['']]
  end

  get '/fail/:number' do
    if fail_count >= params[:number].to_i
      "ok"
    else
      fail_count += 1
      error 500, "oh noes!"
    end
  end

  get '/fail_forever' do
    error 500, "oh noes!"
  end

  get '/redirect' do
    redirect '/'
  end

  get '/bad_redirect' do
    redirect '/bad_redirect'
  end

  get '/auth_basic/:username/:password' do
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    # Check that we've got a basic auth, and that it's credentials match the ones
    # provided in the request
    if @auth.provided? && @auth.basic? && @auth.credentials == [ params[:username], params[:password] ]
      # auth is valid - confirm it
      true
    else
      # invalid auth - request the authentication
      response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
      throw(:halt, [401, "Not authorized\n"])
    end
  end

  get '/auth_ntlm' do
    # we're just checking for the existence if NTLM auth header here. It's validation
    # is too troublesome and really doesn't bother is much, it's up to libcurl to make
    # it valid
    response['WWW-Authenticate'] = 'NTLM'
    is_ntlm_auth = /^NTLM/ =~ request.env['HTTP_AUTHORIZATION']
    true if is_ntlm_auth
    throw(:halt, [401, "Not authorized\n"]) if !is_ntlm_auth
  end

  get '/gzipped' do
    req_env = request.env.to_json
    z = Zlib::Deflate.new
    gzipped_env = z.deflate(req_env, Zlib::FINISH)
    z.close
    response['Content-Encoding'] = 'gzip'
    gzipped_env
  end

  get '/**' do
    sleep params["delay"].to_i if params.has_key?("delay")
    request.env.merge!(:body => request.body.read).to_json
  end

  head '/**' do
    sleep params["delay"].to_i if params.has_key?("delay")
  end

  put '/**' do
    request.env.merge!(:body => request.body.read).to_json
  end

  post '/**' do
    request.env.merge!(:body => request.body.read).to_json
  end

  delete '/**' do
    request.env.merge!(:body => request.body.read).to_json
  end

  patch '/**' do
    request.env.merge!(:body => request.body.read).to_json
  end

  options '/**' do
    request.env.merge!(:body => request.body.read).to_json
  end
end