|
| 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 | +} |
0 commit comments