Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1f979184
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1f979184
编写于
10月 15, 2016
作者:
Z
Zach Aysan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make `process` a non-private method and update documentation.
上级
0bf90fad
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
100 addition
and
98 deletion
+100
-98
actionpack/lib/action_dispatch/testing/integration.rb
actionpack/lib/action_dispatch/testing/integration.rb
+100
-98
未找到文件。
actionpack/lib/action_dispatch/testing/integration.rb
浏览文件 @
1f979184
...
...
@@ -11,62 +11,37 @@
module
ActionDispatch
module
Integration
#:nodoc:
module
RequestHelpers
# Performs a GET request with the given parameters.
#
# - +path+: The URI (as a String) on which you want to perform a GET
# request.
# - +params+: The HTTP parameters that you want to pass. This may
# be +nil+,
# a Hash, or a String that is appropriately encoded
# (<tt>application/x-www-form-urlencoded</tt> or
# <tt>multipart/form-data</tt>).
# - +headers+: Additional headers to pass, as a Hash. The headers will be
# merged into the Rack env hash.
# - +env+: Additional env to pass, as a Hash. The headers will be
# merged into the Rack env hash.
#
# This method returns a Response object, which one can use to
# inspect the details of the response. Furthermore, if this method was
# called from an ActionDispatch::IntegrationTest object, then that
# object's <tt>@response</tt> instance variable will point to the same
# response object.
#
# You can also perform POST, PATCH, PUT, DELETE, and HEAD requests with
# +#post+, +#patch+, +#put+, +#delete+, and +#head+.
#
# Example:
#
# get '/feed', params: { since: 201501011400 }
# post '/profile', headers: { "X-Test-Header" => "testvalue" }
# Performs a GET request with the given parameters. See +#process+ for more
# details.
def
get
(
path
,
**
args
)
process
(
:get
,
path
,
**
args
)
end
# Performs a POST request with the given parameters. See +#
get
+ for more
# Performs a POST request with the given parameters. See +#
process
+ for more
# details.
def
post
(
path
,
**
args
)
process
(
:post
,
path
,
**
args
)
end
# Performs a PATCH request with the given parameters. See +#
get
+ for more
# Performs a PATCH request with the given parameters. See +#
process
+ for more
# details.
def
patch
(
path
,
**
args
)
process
(
:patch
,
path
,
**
args
)
end
# Performs a PUT request with the given parameters. See +#
get
+ for more
# Performs a PUT request with the given parameters. See +#
process
+ for more
# details.
def
put
(
path
,
**
args
)
process
(
:put
,
path
,
**
args
)
end
# Performs a DELETE request with the given parameters. See +#
get
+ for
# Performs a DELETE request with the given parameters. See +#
process
+ for
# more details.
def
delete
(
path
,
**
args
)
process
(
:delete
,
path
,
**
args
)
end
# Performs a HEAD request with the given parameters. See +#
get
+ for more
# Performs a HEAD request with the given parameters. See +#
process
+ for more
# details.
def
head
(
path
,
*
args
)
process
(
:head
,
path
,
*
args
)
...
...
@@ -198,91 +173,118 @@ def https?
@https
end
#
Set the host name to use in the next
request.
#
Performs the actual
request.
#
# session.host! "www.example.com"
alias
:host!
:host
=
private
def
_mock_session
@_mock_session
||=
Rack
::
MockSession
.
new
(
@app
,
host
)
# - +method+: The HTTP method (GET, POST, PATCH, PUT, DELETE, HEAD, OPTIONS)
# as a symbol.
# - +path+: The URI (as a String) on which you want to perform a GET
# request.
# - +params+: The HTTP parameters that you want to pass. This may
# be +nil+,
# a Hash, or a String that is appropriately encoded
# (<tt>application/x-www-form-urlencoded</tt> or
# <tt>multipart/form-data</tt>).
# - +headers+: Additional headers to pass, as a Hash. The headers will be
# merged into the Rack env hash.
# - +env+: Additional env to pass, as a Hash. The headers will be
# merged into the Rack env hash.
#
# This method is rarely used directly. Use +#get+, +#post+, or other standard
# HTTP methods in integration tests. Only +#process+ is only required for an
# OPTIONS request.
#
# This method returns a Response object, which one can use to
# inspect the details of the response. Furthermore, if this method was
# called from an ActionDispatch::IntegrationTest object, then that
# object's <tt>@response</tt> instance variable will point to the same
# response object.
#
# Examples:
# process :get, '/author', params: { since: 201501011400 }
def
process
(
method
,
path
,
params:
nil
,
headers:
nil
,
env:
nil
,
xhr:
false
,
as:
nil
)
request_encoder
=
RequestEncoder
.
encoder
(
as
)
headers
||=
{}
if
method
==
:get
&&
as
==
:json
&&
params
headers
[
"X-Http-Method-Override"
]
=
"GET"
method
=
:post
end
# Performs the actual request.
def
process
(
method
,
path
,
params:
nil
,
headers:
nil
,
env:
nil
,
xhr:
false
,
as:
nil
)
request_encoder
=
RequestEncoder
.
encoder
(
as
)
headers
||=
{}
if
path
=~
%r{://}
path
=
build_expanded_path
(
path
,
request_encoder
)
do
|
location
|
https!
URI
::
HTTPS
===
location
if
location
.
scheme
if
method
==
:get
&&
as
==
:json
&&
params
headers
[
"X-Http-Method-Override"
]
=
"GET"
method
=
:post
if
url_host
=
location
.
host
default
=
Rack
::
Request
::
DEFAULT_PORTS
[
location
.
scheme
]
url_host
+=
":
#{
location
.
port
}
"
if
default
!=
location
.
port
host!
url_host
end
end
elsif
as
path
=
build_expanded_path
(
path
,
request_encoder
)
end
if
path
=~
%r{://}
path
=
build_expanded_path
(
path
,
request_encoder
)
do
|
location
|
https!
URI
::
HTTPS
===
location
if
location
.
scheme
hostname
,
port
=
host
.
split
(
":"
)
if
url_host
=
location
.
host
default
=
Rack
::
Request
::
DEFAULT_PORTS
[
location
.
scheme
]
url_host
+=
":
#{
location
.
port
}
"
if
default
!=
location
.
port
host!
url_host
end
end
elsif
as
path
=
build_expanded_path
(
path
,
request_encoder
)
end
request_env
=
{
:method
=>
method
,
:params
=>
request_encoder
.
encode_params
(
params
),
hostname
,
port
=
host
.
split
(
":"
)
"SERVER_NAME"
=>
hostname
,
"SERVER_PORT"
=>
port
||
(
https?
?
"443"
:
"80"
),
"HTTPS"
=>
https?
?
"on"
:
"off"
,
"rack.url_scheme"
=>
https?
?
"https"
:
"http"
,
request_env
=
{
:method
=>
method
,
:params
=>
request_encoder
.
encode_params
(
params
),
"REQUEST_URI"
=>
path
,
"HTTP_HOST"
=>
host
,
"REMOTE_ADDR"
=>
remote_addr
,
"CONTENT_TYPE"
=>
request_encoder
.
content_type
,
"HTTP_ACCEPT"
=>
accept
}
"SERVER_NAME"
=>
hostname
,
"SERVER_PORT"
=>
port
||
(
https?
?
"443"
:
"80"
),
"HTTPS"
=>
https?
?
"on"
:
"off"
,
"rack.url_scheme"
=>
https?
?
"https"
:
"http"
,
wrapped_headers
=
Http
::
Headers
.
from_hash
({})
wrapped_headers
.
merge!
(
headers
)
if
headers
"REQUEST_URI"
=>
path
,
"HTTP_HOST"
=>
host
,
"REMOTE_ADDR"
=>
remote_addr
,
"CONTENT_TYPE"
=>
request_encoder
.
content_type
,
"HTTP_ACCEPT"
=>
accept
}
if
xhr
wrapped_headers
[
"HTTP_X_REQUESTED_WITH"
]
=
"XMLHttpRequest"
wrapped_headers
[
"HTTP_ACCEPT"
]
||=
[
Mime
[
:js
],
Mime
[
:html
],
Mime
[
:xml
],
"text/xml"
,
"*/*"
].
join
(
", "
)
end
wrapped_headers
=
Http
::
Headers
.
from_hash
({})
wrapped_headers
.
merge!
(
headers
)
if
headers
# this modifies the passed request_env directly
if
wrapped_headers
.
present?
Http
::
Headers
.
from_hash
(
request_env
).
merge!
(
wrapped_headers
)
end
if
env
.
present?
Http
::
Headers
.
from_hash
(
request_env
).
merge!
(
env
)
end
if
xhr
wrapped_headers
[
"HTTP_X_REQUESTED_WITH"
]
=
"XMLHttpRequest"
wrapped_headers
[
"HTTP_ACCEPT"
]
||=
[
Mime
[
:js
],
Mime
[
:html
],
Mime
[
:xml
],
"text/xml"
,
"*/*"
].
join
(
", "
)
end
session
=
Rack
::
Test
::
Session
.
new
(
_mock_session
)
# this modifies the passed request_env directly
if
wrapped_headers
.
present?
Http
::
Headers
.
from_hash
(
request_env
).
merge!
(
wrapped_headers
)
end
if
env
.
present?
Http
::
Headers
.
from_hash
(
request_env
).
merge!
(
env
)
end
# NOTE: rack-test v0.5 doesn't build a default uri correctly
# Make sure requested path is always a full uri
session
.
request
(
build_full_uri
(
path
,
request_env
),
request_env
)
session
=
Rack
::
Test
::
Session
.
new
(
_mock_session
)
@request_count
+=
1
@request
=
ActionDispatch
::
Request
.
new
(
session
.
last_request
.
env
)
response
=
_mock_session
.
last_response
@response
=
ActionDispatch
::
TestResponse
.
from_response
(
response
)
@response
.
request
=
@request
@html_document
=
nil
@url_options
=
nil
# NOTE: rack-test v0.5 doesn't build a default uri correctly
# Make sure requested path is always a full uri
session
.
request
(
build_full_uri
(
path
,
request_env
),
request_env
)
@controller
=
@request
.
controller_instance
@request_count
+=
1
@request
=
ActionDispatch
::
Request
.
new
(
session
.
last_request
.
env
)
response
=
_mock_session
.
last_response
@response
=
ActionDispatch
::
TestResponse
.
from_response
(
response
)
@response
.
request
=
@request
@html_document
=
nil
@url_options
=
nil
response
.
status
end
@controller
=
@request
.
controller_instance
# Set the host name to use in the next request.
#
# session.host! "www.example.com"
alias
:host!
:host
=
response
.
status
private
def
_mock_session
@_mock_session
||=
Rack
::
MockSession
.
new
(
@app
,
host
)
end
def
build_full_uri
(
path
,
env
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录