File: fake_app.rb

package info (click to toggle)
ruby-rack-test 0.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 296 kB
  • sloc: ruby: 1,900; makefile: 5
file content (154 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (3)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require "rubygems"
require "sinatra/base"

module Rack
  module Test

    class FakeApp < Sinatra::Base
      head "/" do
        "meh"
      end

      options "/" do
        [200, {}, ""]
      end

      get "/" do
        "Hello, GET: #{params.inspect}"
      end

      get "/redirect" do
        redirect "/redirected"
      end

      post "/redirect" do
        if params["status"]
          redirect to("/redirected"), Integer(params["status"])
        else
          redirect "/redirected"
        end
      end

      [:get, :put, :post, :delete].each do |meth|
        send(meth, "/redirected") do
          additional_info = (meth == :get) ? "" : " using #{meth} with #{params}"
          "You've been redirected" + additional_info
        end
      end

      get "/void" do
        [200, {}, ""]
      end

      get "/cookies/show" do
        request.cookies.inspect
      end

      get "/COOKIES/show" do
        request.cookies.inspect
      end

      get "/not-cookies/show" do
        request.cookies.inspect
      end

      get "/cookies/set-secure" do
        raise if params["value"].nil?

        response.set_cookie("secure-cookie", :value => params["value"], :secure => true)
        "Set"
      end

      get "/cookies/set-simple" do
        raise if params["value"].nil?

        response.set_cookie "simple", params["value"]
        "Set"
      end

      post "/cookies/default-path" do
        raise if params["value"].nil?

        response.set_cookie "simple", params["value"]
        "Set"
      end

      get "/cookies/default-path" do
        response.cookies.inspect
      end

      get "/cookies/delete" do
        response.delete_cookie "value"
      end

      get "/cookies/count" do
        old_value = request.cookies["count"].to_i || 0
        new_value = (old_value + 1).to_s

        response.set_cookie("count", :value => new_value)
        new_value
      end

      get "/cookies/set" do
        raise if params["value"].nil?

        response.set_cookie("value", {
          :value => params["value"],
          :path => "/cookies",
          :expires => Time.now + 10
        })
        "Set"
      end

      get "/cookies/domain" do
        old_value = request.cookies["count"].to_i || 0
        new_value = (old_value + 1).to_s

        response.set_cookie("count", :value => new_value, :domain => "localhost.com")
        new_value
      end

      get "/cookies/subdomain" do
        old_value = request.cookies["count"].to_i || 0
        new_value = (old_value + 1).to_s

        response.set_cookie("count", :value => new_value, :domain => ".example.org")
        new_value
      end

      get "/cookies/set-uppercase" do
        raise if params["value"].nil?

        response.set_cookie("VALUE", {
          :value => params["value"],
          :path => "/cookies",
          :expires => Time.now + 10
        })
        "Set"
      end

      get "/cookies/set-multiple" do
        response.set_cookie("key1", :value => "value1")
        response.set_cookie("key2", :value => "value2")
        "Set"
      end

      post "/" do
        "Hello, POST: #{params.inspect}"
      end

      put "/" do
        "Hello, PUT: #{params.inspect}"
      end

      patch "/" do
        "Hello, PUT: #{params.inspect}"
      end

      delete "/" do
        "Hello, DELETE: #{params.inspect}"
      end
    end

  end
end