forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContractTest.java
More file actions
149 lines (123 loc) · 5.88 KB
/
ContractTest.java
File metadata and controls
149 lines (123 loc) · 5.88 KB
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
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.Test;
import java.net.URI;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import feign.RequestTemplate.Body;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static feign.Contract.parseAndValidatateMetadata;
import static javax.ws.rs.HttpMethod.DELETE;
import static javax.ws.rs.HttpMethod.GET;
import static javax.ws.rs.HttpMethod.POST;
import static javax.ws.rs.HttpMethod.PUT;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
/**
* Tests interfaces defined per {@link Contract} are interpreted into expected {@link RequestTemplate template}
* instances.
*/
@Test
public class ContractTest {
interface Methods {
@POST void post();
@PUT void put();
@GET void get();
@DELETE void delete();
}
@Test public void httpMethods() throws Exception {
assertEquals(parseAndValidatateMetadata(Methods.class.getDeclaredMethod("post")).template().method(), POST);
assertEquals(parseAndValidatateMetadata(Methods.class.getDeclaredMethod("put")).template().method(), PUT);
assertEquals(parseAndValidatateMetadata(Methods.class.getDeclaredMethod("get")).template().method(), GET);
assertEquals(parseAndValidatateMetadata(Methods.class.getDeclaredMethod("delete")).template().method(), DELETE);
}
interface WithQueryParamsInPath {
@GET @Path("/?Action=GetUser&Version=2010-05-08") Response get();
}
@Test public void queryParamsInPathExtract() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("get"));
assertEquals(md.template().url(), "/");
assertEquals(md.template().queries().get("Action"), ImmutableSet.of("GetUser"));
assertEquals(md.template().queries().get("Version"), ImmutableSet.of("2010-05-08"));
}
interface BodyWithoutParameters {
@POST @Produces(APPLICATION_XML) @Body("<v01:getAccountsListOfUser/>") Response post();
}
@Test public void bodyWithoutParameters() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(BodyWithoutParameters.class.getDeclaredMethod("post"));
assertEquals(md.template().body().get(), "<v01:getAccountsListOfUser/>");
assertFalse(md.template().bodyTemplate().isPresent());
assertTrue(md.formParams().isEmpty());
assertTrue(md.indexToName().isEmpty());
}
@Test public void producesAddsContentTypeHeader() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(BodyWithoutParameters.class.getDeclaredMethod("post"));
assertEquals(md.template().headers().get(CONTENT_TYPE), ImmutableSet.of(APPLICATION_XML));
}
interface WithURIParam {
@GET @Path("/{1}/{2}") Response uriParam(@PathParam("1") String one, URI endpoint, @PathParam("2") String two);
}
@Test public void methodCanHaveUriParam() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(WithURIParam.class.getDeclaredMethod("uriParam", String.class,
URI.class, String.class));
assertEquals(md.urlIndex(), Integer.valueOf(1));
}
@Test public void pathParamsParseIntoIndexToName() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(WithURIParam.class.getDeclaredMethod("uriParam", String.class,
URI.class, String.class));
assertEquals(md.template().url(), "/{1}/{2}");
assertEquals(md.indexToName().get(0), ImmutableSet.of("1"));
assertEquals(md.indexToName().get(2), ImmutableSet.of("2"));
}
interface FormParams {
@POST
@Body("%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D")
void login(
@FormParam("customer_name") String customer,
@FormParam("user_name") String user, @FormParam("password") String password);
}
@Test public void formParamsParseIntoIndexToName() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(FormParams.class.getDeclaredMethod("login", String.class,
String.class, String.class));
assertFalse(md.template().body().isPresent());
assertEquals(md.template().bodyTemplate().get(),
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D");
assertEquals(md.formParams(), ImmutableList.of("customer_name", "user_name", "password"));
assertEquals(md.indexToName().get(0), ImmutableSet.of("customer_name"));
assertEquals(md.indexToName().get(1), ImmutableSet.of("user_name"));
assertEquals(md.indexToName().get(2), ImmutableSet.of("password"));
}
interface HeaderParams {
@POST void logout(@HeaderParam("Auth-Token") String token);
}
@Test public void headerParamsParseIntoIndexToName() throws Exception {
MethodMetadata md = parseAndValidatateMetadata(HeaderParams.class.getDeclaredMethod("logout", String.class));
assertEquals(md.template().headers().get("Auth-Token"), ImmutableSet.of("{Auth-Token}"));
assertEquals(md.indexToName().get(0), ImmutableSet.of("Auth-Token"));
}
}