Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
128a1f8e
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
128a1f8e
编写于
12月 13, 2016
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish MockRestRequestMatchers query param support
Issue: SPR-14995
上级
ba826f10
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
69 addition
and
136 deletion
+69
-136
spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java
...mework/test/web/client/match/MockRestRequestMatchers.java
+47
-70
spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java
...k/test/web/client/match/MockRestRequestMatchersTests.java
+22
-66
未找到文件。
spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java
浏览文件 @
128a1f8e
...
...
@@ -19,13 +19,13 @@ package org.springframework.test.web.client.match;
import
java.io.IOException
;
import
java.net.URI
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
javax.xml.xpath.XPathExpressionException
;
import
org.hamcrest.Matcher
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.client.ClientHttpRequest
;
import
org.springframework.test.util.AssertionErrors
;
...
...
@@ -38,6 +38,7 @@ import org.springframework.web.util.UriUtils;
import
static
java
.
nio
.
charset
.
StandardCharsets
.
UTF_8
;
import
static
org
.
hamcrest
.
MatcherAssert
.*;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
springframework
.
test
.
util
.
AssertionErrors
.*;
/**
...
...
@@ -65,6 +66,21 @@ public abstract class MockRestRequestMatchers {
};
}
/**
* Assert the {@link HttpMethod} of the request.
* @param method the HTTP method
* @return the request matcher
*/
public
static
RequestMatcher
method
(
final
HttpMethod
method
)
{
Assert
.
notNull
(
method
,
"'method' must not be null"
);
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
throws
AssertionError
{
AssertionErrors
.
assertEquals
(
"Unexpected HttpMethod"
,
method
,
request
.
getMethod
());
}
};
}
/**
* Assert the request URI string with the given matcher.
* @param matcher String matcher for the expected URI
...
...
@@ -95,21 +111,6 @@ public abstract class MockRestRequestMatchers {
};
}
/**
* Assert the {@link HttpMethod} of the request.
* @param method the HTTP method
* @return the request matcher
*/
public
static
RequestMatcher
method
(
final
HttpMethod
method
)
{
Assert
.
notNull
(
method
,
"'method' must not be null"
);
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
throws
AssertionError
{
AssertionErrors
.
assertEquals
(
"Unexpected HttpMethod"
,
method
,
request
.
getMethod
());
}
};
}
/**
* Expect a request to the given URI.
* @param uri the expected URI
...
...
@@ -126,110 +127,86 @@ public abstract class MockRestRequestMatchers {
}
/**
* Assert request
head
er values with the given Hamcrest matcher.
* Assert request
query paramet
er values with the given Hamcrest matcher.
*/
@SafeVarargs
public
static
RequestMatcher
header
(
final
String
name
,
final
Matcher
<?
super
String
>...
matchers
)
{
public
static
RequestMatcher
queryParam
(
final
String
name
,
final
Matcher
<?
super
String
>...
matchers
)
{
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
{
assertHeaderValueCount
(
name
,
request
.
getHeaders
(),
matchers
.
length
);
MultiValueMap
<
String
,
String
>
params
=
getQueryParams
(
request
);
assertValueCount
(
"query param"
,
name
,
params
,
matchers
.
length
);
for
(
int
i
=
0
;
i
<
matchers
.
length
;
i
++)
{
assertThat
(
"
Request header"
,
request
.
getHeaders
()
.
get
(
name
).
get
(
i
),
matchers
[
i
]);
assertThat
(
"
Query param"
,
params
.
get
(
name
).
get
(
i
),
matchers
[
i
]);
}
}
};
}
/**
* Assert request
head
er values.
* Assert request
query paramet
er values.
*/
public
static
RequestMatcher
header
(
final
String
name
,
final
String
...
expectedValues
)
{
public
static
RequestMatcher
queryParam
(
final
String
name
,
final
String
...
expectedValues
)
{
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
{
assertHeaderValueCount
(
name
,
request
.
getHeaders
(),
expectedValues
.
length
);
MultiValueMap
<
String
,
String
>
params
=
getQueryParams
(
request
);
assertValueCount
(
"query param"
,
name
,
params
,
expectedValues
.
length
);
for
(
int
i
=
0
;
i
<
expectedValues
.
length
;
i
++)
{
assertEquals
(
"Request header + ["
+
name
+
"]"
,
expectedValues
[
i
],
request
.
getHeaders
().
get
(
name
).
get
(
i
));
assertEquals
(
"Query param + ["
+
name
+
"]"
,
expectedValues
[
i
],
params
.
get
(
name
).
get
(
i
));
}
}
};
}
private
static
void
assertHeaderValueCount
(
final
String
name
,
HttpHeaders
headers
,
int
expectedCoun
t
)
{
assertMultiValueMapCount
(
"header"
,
name
,
headers
,
expectedCount
);
private
static
MultiValueMap
<
String
,
String
>
getQueryParams
(
ClientHttpRequest
reques
t
)
{
return
UriComponentsBuilder
.
fromUri
(
request
.
getURI
()).
build
().
getQueryParams
(
);
}
private
static
void
assertValueCount
(
String
valueType
,
final
String
name
,
MultiValueMap
<
String
,
String
>
map
,
int
count
)
{
/**
* Assert request query parameter values with the given Hamcrest matcher.
*/
@SafeVarargs
public
static
RequestMatcher
queryParameter
(
final
String
name
,
final
Matcher
<?
super
String
>...
matchers
)
{
return
queryParameter
(
name
,
UTF_8
,
matchers
);
List
<
String
>
values
=
map
.
get
(
name
);
String
message
=
"Expected "
+
valueType
+
" <"
+
name
+
">"
;
assertNotNull
(
message
,
values
);
assertTrue
(
message
+
" to have at least <"
+
count
+
"> values but found "
+
values
,
count
<=
values
.
size
());
}
/**
* Assert request
query paramet
er values with the given Hamcrest matcher.
* Assert request
head
er values with the given Hamcrest matcher.
*/
@SafeVarargs
public
static
RequestMatcher
queryParameter
(
final
String
name
,
final
Charset
charset
,
final
Matcher
<?
super
String
>...
matchers
)
{
public
static
RequestMatcher
header
(
final
String
name
,
final
Matcher
<?
super
String
>...
matchers
)
{
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
{
MultiValueMap
<
String
,
String
>
queryParameters
=
getQueryParameters
(
request
.
getURI
().
toString
(),
charset
);
assertQueryParameterValueCount
(
name
,
queryParameters
,
matchers
.
length
);
assertValueCount
(
"header"
,
name
,
request
.
getHeaders
(),
matchers
.
length
);
for
(
int
i
=
0
;
i
<
matchers
.
length
;
i
++)
{
assertThat
(
"Request
query parameter"
,
queryParameters
.
get
(
name
).
get
(
i
),
matchers
[
i
]);
assertThat
(
"Request
header"
,
request
.
getHeaders
()
.
get
(
name
).
get
(
i
),
matchers
[
i
]);
}
}
};
}
/**
* Assert request query parameter values.
*/
public
static
RequestMatcher
queryParameter
(
final
String
name
,
final
String
...
expectedValues
)
{
return
queryParameter
(
name
,
UTF_8
,
expectedValues
);
}
/**
* Assert request query parameter values.
* Assert request header values.
*/
public
static
RequestMatcher
queryParameter
(
final
String
name
,
final
Charset
charset
,
final
String
...
expectedValues
)
{
public
static
RequestMatcher
header
(
final
String
name
,
final
String
...
expectedValues
)
{
return
new
RequestMatcher
()
{
@Override
public
void
match
(
ClientHttpRequest
request
)
{
MultiValueMap
<
String
,
String
>
queryParameters
=
getQueryParameters
(
request
.
getURI
().
toString
(),
charset
);
assertQueryParameterValueCount
(
name
,
queryParameters
,
expectedValues
.
length
);
assertValueCount
(
"header"
,
name
,
request
.
getHeaders
(),
expectedValues
.
length
);
for
(
int
i
=
0
;
i
<
expectedValues
.
length
;
i
++)
{
assertEquals
(
"Request
query paramet
er + ["
+
name
+
"]"
,
expectedValues
[
i
],
queryParameters
.
get
(
name
).
get
(
i
));
assertEquals
(
"Request
head
er + ["
+
name
+
"]"
,
expectedValues
[
i
],
request
.
getHeaders
()
.
get
(
name
).
get
(
i
));
}
}
};
}
private
static
MultiValueMap
<
String
,
String
>
getQueryParameters
(
String
uri
,
Charset
charset
)
{
String
decodeUri
=
UriUtils
.
decode
(
uri
,
charset
);
MultiValueMap
<
String
,
String
>
queryParameters
=
UriComponentsBuilder
.
fromUriString
(
decodeUri
)
.
build
().
getQueryParams
();
return
queryParameters
;
}
private
static
void
assertQueryParameterValueCount
(
final
String
name
,
MultiValueMap
<
String
,
String
>
queryParameters
,
int
expectedCount
)
{
assertMultiValueMapCount
(
"query parameter"
,
name
,
queryParameters
,
expectedCount
);
}
private
static
void
assertMultiValueMapCount
(
String
type
,
final
String
name
,
MultiValueMap
<
String
,
String
>
multiValueMap
,
int
expectedCount
)
{
List
<
String
>
actualValues
=
multiValueMap
.
get
(
name
);
assertTrue
(
"Expected "
+
type
+
" <"
+
name
+
">"
,
actualValues
!=
null
);
assertTrue
(
"Expected "
+
type
+
" <"
+
name
+
"> to have at least <"
+
expectedCount
+
"> values but found "
+
actualValues
,
expectedCount
<=
actualValues
.
size
());
}
/**
* Access to request body matchers.
*/
...
...
spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java
浏览文件 @
128a1f8e
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
6
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,20 +16,16 @@
package
org.springframework.test.web.client.match
;
import
java.net.URI
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
org.junit.Test
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.mock.http.client.MockClientHttpRequest
;
import
org.springframework.test.web.client.RequestMatcher
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
java.net.URI
;
import
java.util.Arrays
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
notNullValue
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Unit tests for {@link MockRestRequestMatchers}.
...
...
@@ -129,79 +125,39 @@ public class MockRestRequestMatchersTests {
@Test
(
expected
=
AssertionError
.
class
)
public
void
headersWithMissingValue
()
throws
Exception
{
this
.
request
.
getHeaders
().
put
(
"foo"
,
Arrays
.
as
List
(
"bar"
));
this
.
request
.
getHeaders
().
put
(
"foo"
,
Collections
.
singleton
List
(
"bar"
));
MockRestRequestMatchers
.
header
(
"foo"
,
"bar"
,
"baz"
).
match
(
this
.
request
);
}
@Test
public
void
queryParameter
()
throws
Exception
{
this
.
request
.
setURI
(
createUriWithQueryParameters
(
"foo"
,
"bar"
,
"baz"
));
RequestMatcher
requestMatcher
=
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
"bar"
,
"baz"
);
assertThat
(
"Factory method did not create any request matcher"
,
requestMatcher
,
notNullValue
());
requestMatcher
.
match
(
this
.
request
);
public
void
queryParam
()
throws
Exception
{
this
.
request
.
setURI
(
new
URI
(
"http://foo.com/a?foo=bar&foo=baz"
));
MockRestRequestMatchers
.
queryParam
(
"foo"
,
"bar"
,
"baz"
).
match
(
this
.
request
);
}
@Test
(
expected
=
AssertionError
.
class
)
public
void
queryParameterMissing
()
throws
Exception
{
this
.
request
.
setURI
(
UriComponentsBuilder
.
fromUriString
(
"http://foo.com"
)
.
path
(
"/bar"
)
.
build
().
encode
()
.
toUri
());
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
"bar"
).
match
(
this
.
request
);
public
void
queryParamMissing
()
throws
Exception
{
this
.
request
.
setURI
(
new
URI
(
"http://foo.com/a"
));
MockRestRequestMatchers
.
queryParam
(
"foo"
,
"bar"
).
match
(
this
.
request
);
}
@Test
(
expected
=
AssertionError
.
class
)
public
void
queryParameterMissingValue
()
throws
Exception
{
this
.
request
.
setURI
(
createUriWithQueryParameters
(
"foo"
,
"bar"
,
"baz"
));
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
"bad"
).
match
(
this
.
request
);
public
void
queryParamMissingValue
()
throws
Exception
{
this
.
request
.
setURI
(
new
URI
(
"http://foo.com/a?foo=bar&foo=baz"
));
MockRestRequestMatchers
.
queryParam
(
"foo"
,
"bad"
).
match
(
this
.
request
);
}
@Test
public
void
queryParameterContains
()
throws
Exception
{
this
.
request
.
setURI
(
createUriWithQueryParameters
(
"foo"
,
"bar"
,
"baz"
));
RequestMatcher
requestMatcher
=
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
containsString
(
"ba"
));
assertThat
(
"Factory method did not create any request matcher"
,
requestMatcher
,
notNullValue
());
requestMatcher
.
match
(
this
.
request
);
public
void
queryParamContains
()
throws
Exception
{
this
.
request
.
setURI
(
new
URI
(
"http://foo.com/a?foo=bar&foo=baz"
));
MockRestRequestMatchers
.
queryParam
(
"foo"
,
containsString
(
"ba"
)).
match
(
this
.
request
);
}
@Test
(
expected
=
AssertionError
.
class
)
public
void
queryParameterContainsWithMissingValue
()
throws
Exception
{
this
.
request
.
setURI
(
createUriWithQueryParameters
(
"foo"
,
"bar"
,
"baz"
));
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
containsString
(
"bx"
)).
match
(
this
.
request
);
}
@Test
public
void
queryParameters
()
throws
Exception
{
this
.
request
.
setURI
(
createUriWithQueryParameters
(
"foo"
,
"bar"
,
"baz"
));
RequestMatcher
requestMatcher
=
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
"bar"
,
"baz"
);
assertThat
(
"Factory method did not create any request matcher"
,
requestMatcher
,
notNullValue
());
requestMatcher
.
match
(
this
.
request
);
}
@Test
(
expected
=
AssertionError
.
class
)
public
void
queryParametersWithMissingValue
()
throws
Exception
{
this
.
request
.
setURI
(
createUriWithQueryParameters
(
"foo"
,
"bar"
));
MockRestRequestMatchers
.
queryParameter
(
"foo"
,
"bar"
,
"baz"
).
match
(
this
.
request
);
}
private
URI
createUriWithQueryParameters
(
String
key
,
String
...
values
)
{
MultiValueMap
<
String
,
String
>
params
=
new
LinkedMultiValueMap
<>();
params
.
put
(
key
,
Arrays
.
asList
(
values
));
return
UriComponentsBuilder
.
fromUriString
(
"http://foo.com"
)
.
path
(
"/bar"
)
.
queryParams
(
params
)
.
build
().
encode
().
toUri
();
public
void
queryParamContainsWithMissingValue
()
throws
Exception
{
this
.
request
.
setURI
(
new
URI
(
"http://foo.com/a?foo=bar&foo=baz"
));
MockRestRequestMatchers
.
queryParam
(
"foo"
,
containsString
(
"bx"
)).
match
(
this
.
request
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录