File: testrequest.rb

package info (click to toggle)
ruby-rack 2.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,492 kB
  • sloc: ruby: 14,778; sh: 12; makefile: 6; javascript: 1
file content (80 lines) | stat: -rw-r--r-- 2,012 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
# frozen_string_literal: true

require 'yaml'
require 'net/http'
require 'rack/lint'

class TestRequest
  NOSERIALIZE = [Method, Proc, Rack::Lint::InputWrapper]

  def call(env)
    status = env["QUERY_STRING"] =~ /secret/ ? 403 : 200
    env["test.postdata"] = env["rack.input"].read
    minienv = env.dup
    # This may in the future want to replace with a dummy value instead.
    minienv.delete_if { |k, v| NOSERIALIZE.any? { |c| v.kind_of?(c) } }
    body = minienv.to_yaml
    size = body.bytesize
    [status, { "Content-Type" => "text/yaml", "Content-Length" => size.to_s }, [body]]
  end

  module Helpers
    attr_reader :status, :response

    ROOT = File.expand_path(File.dirname(__FILE__) + "/..")
    ENV["RUBYOPT"] = "-I#{ROOT}/lib -rubygems"

    def root
      ROOT
    end

    def rackup
      "#{ROOT}/bin/rackup"
    end

    def GET(path, header = {})
      Net::HTTP.start(@host, @port) { |http|
        user = header.delete(:user)
        passwd = header.delete(:passwd)

        get = Net::HTTP::Get.new(path, header)
        get.basic_auth user, passwd  if user && passwd
        http.request(get) { |response|
          @status = response.code.to_i
          begin
            @response = YAML.load(response.body)
          rescue TypeError, ArgumentError
            @response = nil
          end
        }
      }
    end

    def POST(path, formdata = {}, header = {})
      Net::HTTP.start(@host, @port) { |http|
        user = header.delete(:user)
        passwd = header.delete(:passwd)

        post = Net::HTTP::Post.new(path, header)
        post.form_data = formdata
        post.basic_auth user, passwd  if user && passwd
        http.request(post) { |response|
          @status = response.code.to_i
          @response = YAML.load(response.body)
        }
      }
    end
  end
end

class StreamingRequest
  def self.call(env)
    [200, { "Content-Type" => "text/plain" }, new]
  end

  def each
    yield "hello there!\n"
    sleep 5
    yield "that is all.\n"
  end
end