Skip to content

Commit 9f90683

Browse files
committed
Test for success or error
1 parent cc880c5 commit 9f90683

File tree

4 files changed

+128
-65
lines changed

4 files changed

+128
-65
lines changed

src/main/java/net/codestory/rest/RestAssert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Request authenticateProxy(Proxy proxy, com.squareup.okhttp.Response respo
9090
// Assertions
9191
public Should should() {
9292
try {
93-
return new Should(call(), false);
93+
return new RestResponseShould(call(), false);
9494
} catch (IOException e) {
9595
throw new RuntimeException("Unable to query: " + url, e);
9696
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright (C) 2013-2014 [email protected]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License
15+
*/
16+
package net.codestory.rest;
17+
18+
import java.util.Objects;
19+
20+
import static java.lang.String.format;
21+
22+
class RestResponseShould implements Should {
23+
private final RestResponse response;
24+
private final boolean negate;
25+
26+
RestResponseShould(RestResponse response, boolean negate) {
27+
this.response = response;
28+
this.negate = negate;
29+
}
30+
31+
// Modifiers
32+
33+
@Override
34+
public RestResponseShould not() {
35+
return new RestResponseShould(response, !negate);
36+
}
37+
38+
// Verifications
39+
40+
// TODO: shouldn't be able to write should().should()
41+
@Override
42+
public RestResponseShould should() {
43+
return new RestResponseShould(response, false);
44+
}
45+
46+
@Override
47+
public RestResponseShould respond(int statusCode) {
48+
return assertEquals("status code", response.code(), statusCode);
49+
}
50+
51+
@Override
52+
public RestResponseShould succeed() {
53+
return assertBetween("status code", response.code(), 200, 299);
54+
}
55+
56+
@Override
57+
public RestResponseShould fail() {
58+
return assertBetween("status code", response.code(), 400, 599);
59+
}
60+
61+
@Override
62+
public RestResponseShould contain(String content) {
63+
return assertContains(response.bodyAsString(), content);
64+
}
65+
66+
@Override
67+
public RestResponseShould beEmpty() {
68+
return assertEmpty(response.bodyAsString());
69+
}
70+
71+
@Override
72+
public RestResponseShould haveType(String contentType) {
73+
return assertContains(response.contentType(), contentType);
74+
}
75+
76+
@Override
77+
public RestResponseShould haveCookie(String name, String value) {
78+
return assertEquals("cookie " + name, response.cookie(name), value);
79+
}
80+
81+
@Override
82+
public RestResponseShould haveHeader(String name, String value) {
83+
return assertEquals("header " + name, response.header(name), value);
84+
}
85+
86+
// Verifications
87+
private RestResponseShould assertEquals(String what, Object actual, Object expected) {
88+
if (negate == Objects.equals(expected, actual)) {
89+
throw new AssertionError(format("Expecting [%s] to be [%s]. It was [%s]", what, expected, actual));
90+
}
91+
return this;
92+
}
93+
94+
private RestResponseShould assertContains(String actual, String expected) {
95+
if (negate == actual.contains(expected)) {
96+
throw new AssertionError(format("Expecting [%s] to contain [%s]", actual, expected));
97+
}
98+
return this;
99+
}
100+
101+
private RestResponseShould assertEmpty(String actual) {
102+
if (negate == actual.isEmpty()) {
103+
throw new AssertionError(format("Expecting [%s] to be empty", actual));
104+
}
105+
return this;
106+
}
107+
108+
private RestResponseShould assertBetween(String what, int actual, int lowerBound, int higherBound) {
109+
if (negate == ((actual >= lowerBound) && (actual <= higherBound))) {
110+
throw new AssertionError(format("Expecting [%s] to be between [%d] and [%d]. It was [%s]", what, lowerBound, higherBound, actual));
111+
}
112+
return this;
113+
}
114+
}

src/main/java/net/codestory/rest/Should.java

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,24 @@
1515
*/
1616
package net.codestory.rest;
1717

18-
import java.util.Objects;
18+
public interface Should {
19+
Should should();
1920

20-
import static java.lang.String.format;
21+
Should not();
2122

22-
public class Should {
23-
private final RestResponse response;
24-
private final boolean negate;
23+
Should respond(int statusCode);
2524

26-
Should(RestResponse response, boolean negate) {
27-
this.response = response;
28-
this.negate = negate;
29-
}
25+
Should succeed();
3026

31-
// Modifiers
27+
Should fail();
3228

33-
public Should not() {
34-
return new Should(response, !negate);
35-
}
29+
Should contain(String content);
3630

37-
// Verifications
31+
Should beEmpty();
3832

39-
// TODO: shouldn't be able to write should().should()
40-
public Should should() {
41-
return new Should(response, false);
42-
}
33+
Should haveType(String contentType);
4334

44-
public Should respond(int statusCode) {
45-
return assertEquals("status code", response.code(), statusCode);
46-
}
35+
Should haveCookie(String name, String value);
4736

48-
public Should contain(String content) {
49-
return assertContains(response.bodyAsString(), content);
50-
}
51-
52-
public Should beEmpty() {
53-
return assertEmpty(response.bodyAsString());
54-
}
55-
56-
public Should haveType(String contentType) {
57-
return assertContains(response.contentType(), contentType);
58-
}
59-
60-
public Should haveCookie(String name, String value) {
61-
return assertEquals("cookie " + name, response.cookie(name), value);
62-
}
63-
64-
public Should haveHeader(String name, String value) {
65-
return assertEquals("header " + name, response.header(name), value);
66-
}
67-
68-
// Verifications
69-
private Should assertEquals(String what, Object actual, Object expected) {
70-
if (negate == Objects.equals(expected, actual)) {
71-
throw new AssertionError(format("Expecting [%s] to be [%s]. It was [%s]", what, expected, actual));
72-
}
73-
return this;
74-
}
75-
76-
private Should assertContains(String actual, String expected) {
77-
if (negate == actual.contains(expected)) {
78-
throw new AssertionError(format("Expecting [%s] to contain [%s]", actual, expected));
79-
}
80-
return this;
81-
}
82-
83-
private Should assertEmpty(String actual) {
84-
if (negate == actual.isEmpty()) {
85-
throw new AssertionError(format("Expecting [%s] to be empty", actual));
86-
}
87-
return this;
88-
}
37+
Should haveHeader(String name, String value);
8938
}

src/test/java/net/codestory/rest/GetTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void get() {
2929
.get("/", "hello World")
3030
);
3131

32-
get("/").should().respond(200).haveType("text/html").contain("hello");
32+
get("/").should().respond(200).succeed().haveType("text/html").contain("hello");
3333
}
3434

3535
@Test
@@ -82,7 +82,7 @@ public void get_with_basic_authentication() {
8282

8383
get("/").withAuthentication("login", "pwd").should().contain("Secret");
8484
get("/").withAuthentication("", "").should().respond(401);
85-
get("/").should().respond(401);
85+
get("/").should().respond(401).fail();
8686
}
8787

8888
@Test

0 commit comments

Comments
 (0)