|
| 1 | +/** |
| 2 | + * Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver) |
| 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 org.commonjava.test.http.junit4.expect; |
| 17 | + |
| 18 | +import org.commonjava.test.http.common.CommonMethod; |
| 19 | +import org.commonjava.test.http.expect.ContentResponse; |
| 20 | +import org.commonjava.test.http.expect.ExpectationHandler; |
| 21 | +import org.commonjava.test.http.expect.ExpectationServer; |
| 22 | +import org.junit.rules.ExternalResource; |
| 23 | + |
| 24 | +import java.io.InputStream; |
| 25 | +import java.net.MalformedURLException; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +@SuppressWarnings( "unused" ) |
| 29 | +public class ExpectationServerWrapper |
| 30 | + extends ExternalResource |
| 31 | +{ |
| 32 | + private final ExpectationServer server; |
| 33 | + |
| 34 | + public ExpectationServerWrapper() |
| 35 | + { |
| 36 | + this( null ); |
| 37 | + } |
| 38 | + |
| 39 | + public ExpectationServerWrapper( final String baseResource ) |
| 40 | + { |
| 41 | + this.server = new ExpectationServer( baseResource ); |
| 42 | + } |
| 43 | + |
| 44 | + public ExpectationServerWrapper( final String baseResource, final int port ) |
| 45 | + { |
| 46 | + this.server = new ExpectationServer( baseResource, port ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void after() |
| 51 | + { |
| 52 | + server.stop(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void before() |
| 57 | + { |
| 58 | + server.start(); |
| 59 | + } |
| 60 | + |
| 61 | + public int getPort() |
| 62 | + { |
| 63 | + return server.getPort(); |
| 64 | + } |
| 65 | + |
| 66 | + public String formatUrl( final String... subpath ) |
| 67 | + { |
| 68 | + return server.formatUrl( subpath ); |
| 69 | + } |
| 70 | + |
| 71 | + public String formatPath( final String... subpath ) |
| 72 | + { |
| 73 | + return server.formatPath( subpath ); |
| 74 | + } |
| 75 | + |
| 76 | + public String getBaseUri() |
| 77 | + { |
| 78 | + return server.getBaseUri(); |
| 79 | + } |
| 80 | + |
| 81 | + public String getUrlPath( final String url ) |
| 82 | + throws MalformedURLException |
| 83 | + { |
| 84 | + return server.getUrlPath( url ); |
| 85 | + } |
| 86 | + |
| 87 | + public Map<String, Integer> getAccessesByPathKey() |
| 88 | + { |
| 89 | + return server.getAccessesByPathKey(); |
| 90 | + } |
| 91 | + |
| 92 | + public Map<String, ContentResponse> getRegisteredErrors() |
| 93 | + { |
| 94 | + return server.getRegisteredErrors(); |
| 95 | + } |
| 96 | + |
| 97 | + public void registerException( final String url, final String error ) |
| 98 | + { |
| 99 | + server.registerException( url, error ); |
| 100 | + } |
| 101 | + |
| 102 | + public void registerException( final String method, final String url, final String error ) |
| 103 | + { |
| 104 | + server.registerException( method, url, error ); |
| 105 | + } |
| 106 | + |
| 107 | + public void registerException( final String url, final String error, final int responseCode ) |
| 108 | + { |
| 109 | + server.registerException( url, error, responseCode ); |
| 110 | + } |
| 111 | + |
| 112 | + public void registerException( final String method, final String url, final int responseCode, final String error ) |
| 113 | + { |
| 114 | + server.registerException( method, url, responseCode, error ); |
| 115 | + } |
| 116 | + |
| 117 | + public void expect( final String testUrl, final int responseCode, final String body ) |
| 118 | + throws Exception |
| 119 | + { |
| 120 | + server.expect( testUrl, responseCode, body ); |
| 121 | + } |
| 122 | + |
| 123 | + public void expect( final String method, final String testUrl, final int responseCode, final String body ) |
| 124 | + throws Exception |
| 125 | + { |
| 126 | + server.expect( method, testUrl, responseCode, body ); |
| 127 | + } |
| 128 | + |
| 129 | + public void expect( final String testUrl, final int responseCode, final InputStream bodyStream ) |
| 130 | + throws Exception |
| 131 | + { |
| 132 | + server.expect( testUrl, responseCode, bodyStream ); |
| 133 | + } |
| 134 | + |
| 135 | + public void expect( final String method, final String testUrl, final int responseCode, |
| 136 | + final InputStream bodyStream ) |
| 137 | + throws Exception |
| 138 | + { |
| 139 | + server.expect( method, testUrl, responseCode, bodyStream ); |
| 140 | + } |
| 141 | + |
| 142 | + public void expect( final String method, final String testUrl, ExpectationHandler handler ) |
| 143 | + { |
| 144 | + server.expect( method, testUrl, handler ); |
| 145 | + } |
| 146 | + |
| 147 | + public String getAccessKey( final CommonMethod method, final String path ) |
| 148 | + { |
| 149 | + return server.getAccessKey( method, path ); |
| 150 | + } |
| 151 | + |
| 152 | + public String getAccessKey( final String method, final String path ) |
| 153 | + { |
| 154 | + return server.getAccessKey( method, path ); |
| 155 | + } |
| 156 | + |
| 157 | + public Integer getAccessesFor( final String path ) |
| 158 | + { |
| 159 | + return server.getAccessesFor( path ); |
| 160 | + } |
| 161 | + |
| 162 | + public Integer getAccessesFor( final String method, final String path ) |
| 163 | + { |
| 164 | + return server.getAccessesFor( method, path ); |
| 165 | + } |
| 166 | + |
| 167 | + public ExpectationServer getServer() |
| 168 | + { |
| 169 | + return server; |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments