File: test_response.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (58 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

# :markup: markdown

require "action_dispatch/testing/request_encoder"

module ActionDispatch
  # Integration test methods such as Integration::RequestHelpers#get and
  # Integration::RequestHelpers#post return objects of class TestResponse, which
  # represent the HTTP response results of the requested controller actions.
  #
  # See Response for more information on controller response objects.
  class TestResponse < Response
    def self.from_response(response)
      new response.status, response.headers, response.body
    end

    # Returns a parsed body depending on the response MIME type. When a parser
    # corresponding to the MIME type is not found, it returns the raw body.
    #
    # #### Examples
    #     get "/posts"
    #     response.content_type         # => "text/html; charset=utf-8"
    #     response.parsed_body.class    # => Nokogiri::HTML5::Document
    #     response.parsed_body.to_html  # => "<!DOCTYPE html>\n<html>\n..."
    #
    #     assert_pattern { response.parsed_body.at("main") => { content: "Hello, world" } }
    #
    #     response.parsed_body.at("main") => {name:, content:}
    #     assert_equal "main", name
    #     assert_equal "Some main content", content
    #
    #     get "/posts.json"
    #     response.content_type         # => "application/json; charset=utf-8"
    #     response.parsed_body.class    # => Array
    #     response.parsed_body          # => [{"id"=>42, "title"=>"Title"},...
    #
    #     assert_pattern { response.parsed_body => [{ id: 42 }] }
    #
    #     get "/posts/42.json"
    #     response.content_type         # => "application/json; charset=utf-8"
    #     response.parsed_body.class    # => ActiveSupport::HashWithIndifferentAccess
    #     response.parsed_body          # => {"id"=>42, "title"=>"Title"}
    #
    #     assert_pattern { response.parsed_body => [{ title: /title/i }] }
    #
    #     response.parsed_body => {id:, title:}
    #     assert_equal 42, id
    #     assert_equal "Title", title
    def parsed_body
      @parsed_body ||= response_parser.call(body)
    end

    def response_parser
      @response_parser ||= RequestEncoder.parser(media_type)
    end
  end
end