├── .circleci
└── config.yml
├── .gitignore
├── CHANGELOG.md
├── Jenkinsfile
├── header.txt
├── license.txt
├── pom.xml
├── readme.md
├── release.groovy
└── src
├── main
├── java
│ └── io
│ │ └── fabric8
│ │ └── mockwebserver
│ │ ├── Context.java
│ │ ├── DefaultMockServer.java
│ │ ├── MockServer.java
│ │ ├── MockServerException.java
│ │ ├── ServerRequest.java
│ │ ├── ServerResponse.java
│ │ ├── crud
│ │ ├── Attribute.java
│ │ ├── AttributeExtractor.java
│ │ ├── AttributeSet.java
│ │ ├── AttributeType.java
│ │ ├── CrudDispatcher.java
│ │ ├── Key.java
│ │ ├── ResponseComposer.java
│ │ └── Value.java
│ │ ├── dsl
│ │ ├── DelayPathable.java
│ │ ├── DelayTimesOrOnceable.java
│ │ ├── Delayable.java
│ │ ├── Doneable.java
│ │ ├── Emitable.java
│ │ ├── EventDoneable.java
│ │ ├── Eventable.java
│ │ ├── Failable.java
│ │ ├── Function.java
│ │ ├── HttpHeaderable.java
│ │ ├── HttpMethod.java
│ │ ├── HttpMethodable.java
│ │ ├── HttpStatusable.java
│ │ ├── MockServerExpectation.java
│ │ ├── Onceable.java
│ │ ├── Openable.java
│ │ ├── Pathable.java
│ │ ├── Replyable.java
│ │ ├── ReturnOrWebsocketable.java
│ │ ├── Returnable.java
│ │ ├── Schedulable.java
│ │ ├── TimesOnceableOrHttpHeaderable.java
│ │ ├── TimesOrOnceable.java
│ │ ├── TimesSchedulableOrOnceable.java
│ │ ├── Timesable.java
│ │ ├── WebSocketSessionBuilder.java
│ │ └── WebSocketable.java
│ │ ├── internal
│ │ ├── ChunkedResponse.java
│ │ ├── InlineWebSocketSessionBuilder.java
│ │ ├── MockDispatcher.java
│ │ ├── MockSSLContextFactory.java
│ │ ├── MockServerExpectationImpl.java
│ │ ├── SimpleRequest.java
│ │ ├── SimpleResponse.java
│ │ ├── WebSocketMessage.java
│ │ └── WebSocketSession.java
│ │ └── utils
│ │ ├── BodyProvider.java
│ │ ├── CertUtils.java
│ │ ├── Closeables.java
│ │ ├── PKCS1Util.java
│ │ ├── ResponseProvider.java
│ │ ├── ResponseProviders.java
│ │ └── SSLUtils.java
└── resources
│ └── ssl
│ ├── fabric8
│ ├── fabric8.crt
│ ├── fabric8.csr
│ └── fabric8.pub
└── test
└── groovy
└── io
└── fabric8
└── mockwebserver
├── DefaultMockServerCrudTest.groovy
├── DefaultMockServerHttpsTest.groovy
├── DefaultMockServerTest.groovy
├── DefaultMockServerWebSocketTest.groovy
├── JsonResponseComposer.groovy
├── MockServerExceptionTest.groovy
├── User.groovy
├── UserAttributeExtractor.groovy
└── crud
├── AttributeSetTest.groovy
├── AttributeTest.groovy
└── CrudDispatcherTest.groovy
/.circleci/config.yml:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright (C) 2015 Red Hat, Inc.
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 |
17 | version: 2
18 |
19 | jobs:
20 | build:
21 | working_directory: ~/fabric8io/mockwebserver
22 | machine: true
23 | steps:
24 | - checkout
25 | - run:
26 | name: License check
27 | command: mvn -N license:check
28 | - run:
29 | command: |
30 | mvn clean install
31 | - save_cache:
32 | key: f8-{{ checksum "pom.xml" }}
33 | paths:
34 | - ~/.m2
35 |
36 | workflows:
37 | version: 2
38 | all:
39 | jobs:
40 | - build
41 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | ### Maven template
3 | target/
4 | pom.xml.tag
5 | pom.xml.releaseBackup
6 | pom.xml.versionsBackup
7 | pom.xml.next
8 | release.properties
9 | dependency-reduced-pom.xml
10 | buildNumber.properties
11 | .mvn/timing.properties
12 |
13 |
14 | ### Java template
15 | *.class
16 |
17 | # Mobile Tools for Java (J2ME)
18 | .mtj.tmp/
19 |
20 | # Package Files #
21 | *.jar
22 | *.war
23 | *.ear
24 |
25 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
26 | hs_err_pid*
27 |
28 |
29 | ### JetBrains template
30 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion
31 |
32 | *.iml
33 |
34 | ## Directory-based project format:
35 | .idea/
36 | # if you remove the above rule, at least ignore the following:
37 |
38 | # User-specific stuff:
39 | # .idea/workspace.xml
40 | # .idea/tasks.xml
41 | # .idea/dictionaries
42 |
43 | # Sensitive or high-churn files:
44 | # .idea/dataSources.ids
45 | # .idea/dataSources.xml
46 | # .idea/sqlDataSources.xml
47 | # .idea/dynamic.xml
48 | # .idea/uiDesigner.xml
49 |
50 | # Gradle:
51 | # .idea/gradle.xml
52 | # .idea/libraries
53 |
54 | # Mongo Explorer plugin:
55 | # .idea/mongoSettings.xml
56 |
57 | ## File-based project format:
58 | *.ipr
59 | *.iws
60 |
61 | ## Plugin-specific files:
62 |
63 | # IntelliJ
64 | /out/
65 |
66 | # mpeltonen/sbt-idea plugin
67 | .idea_modules/
68 |
69 | # JIRA plugin
70 | atlassian-ide-plugin.xml
71 |
72 | # Crashlytics plugin (for Android Studio and IntelliJ)
73 | com_crashlytics_export_strings.xml
74 | crashlytics.properties
75 | crashlytics-build.properties
76 |
77 |
78 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## CHANGELOG
2 |
3 | ### 0.3-SNAPSHOT
4 |
5 | #### Bugs
6 | #### Improvements
7 | #### Dependency Upgrade
8 | #### New Feature
9 |
10 | ### 0.2.2 (2021-11-03)
11 |
12 | #### Bugs
13 | * Fix #70: WebSocketSession regression, WS closed when no configured messages
14 |
15 | ### 0.2.1 (2021-11-03)
16 |
17 | #### Bugs
18 | * Fix #69: regression on Attribute#equals & Attribute#hashCode
19 |
20 | ### 0.2.0 (2021-11-03)
21 |
22 | #### Bugs
23 | * Fix #66: Removed code smells and bugs
24 | * Fix #64: Allow concurrent access to Map preserving insertion order
25 | * Fix #57: Update expired SSL certificates (new expiry date 2031-11-01)
26 |
27 | #### Improvements
28 | * Fix #59: adding a getter to access namespace from the path (fabric8io/kubernetes-client#2921)
29 | * Fix #66: Added getLastRequest method to MockServer
30 | * Fix #66: CRUD dispatcher handles PUT independently of POST
31 |
32 | #### Dependency Upgrade
33 | * Fix #52: Updated OkHttpClient to version 3.12.12
34 | * Fix #65: Updated Jackson to version 2.13.0
35 | * Fix #61: Removed Sundrio dependency
36 |
37 | ### 0.1.8 (2020-04-14)
38 | #### Bugs
39 | #### Improvements
40 | #### Dependency Upgrade
41 | * Updated OkHttpClient to version 3.12.6
42 |
43 | #### New Feature
44 |
45 | ### 0.1.7 (2019-09-27)
46 | #### Bugs
47 |
48 | #### Improvements
49 | * AttributeSet now examines all attributes when determining if another AttributeSet matches.
50 | #### Dependency Upgrade
51 |
52 | #### New Feature
53 |
54 |
55 | ### 0.1.6 (26-09-2019)
56 | #### Bugs
57 |
58 | #### Improvements
59 | * Attribute Type matching now supports EXISTS and NOT_EXISTS
60 |
61 | #### Dependency Upgrade
62 |
63 | #### New Feature
64 |
65 |
66 | ### 0.1.4 (16-09-2019)
67 | #### Bugs
68 |
69 | #### Improvements
70 | * Adding argument to getStatusCode
71 |
72 | #### Dependency Upgrade
73 |
74 | #### New Feature
75 |
76 |
77 |
78 | ### 0.1.3 (03-09-2019)
79 | #### Bugs
80 |
81 | #### Improvements
82 | * Added Attribute Types (WITH and WITHOUT) to works with withoutLabel filter (AttributeSet.matches()).
83 |
84 | #### Dependency Upgrade
85 |
86 | #### New Feature
87 |
88 |
--------------------------------------------------------------------------------
/Jenkinsfile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/groovy
2 | @Library('github.com/fabric8io/fabric8-pipeline-library@master')
3 | def utils = new io.fabric8.Utils()
4 |
5 | releaseNode {
6 | try {
7 | checkout scm
8 | readTrusted 'release.groovy'
9 |
10 | if (utils.isCI()) {
11 |
12 | echo 'CI is not handled by pipelines yet'
13 |
14 | } else if (utils.isCD()) {
15 | sh "git remote set-url origin git@github.com:fabric8io/mockwebserver.git"
16 |
17 | def pipeline = load 'release.groovy'
18 | def stagedProject
19 |
20 | stage('Stage') {
21 | stagedProject = pipeline.stage()
22 | }
23 |
24 | stage('Promote') {
25 | pipeline.release(stagedProject)
26 | }
27 |
28 | stage('Promote YAMLs') {
29 | pipeline.promoteYamls(stagedProject[1])
30 | }
31 | }
32 | } catch (err) {
33 | hubot room: 'release', message: "${env.JOB_NAME} failed: ${err}"
34 | error "${err}"
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/header.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) ${project.inceptionYear} ${owner}
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 | 4.0.0
23 |
24 | io.fabric8
25 | mockwebserver
26 | 0.3-SNAPSHOT
27 | Fabric8 :: Mock Web Server
28 | Mock Web Server
29 |
30 | http://fabric8.io/
31 | 2015
32 |
33 |
34 | Red Hat
35 | http://redhat.com
36 |
37 |
38 |
39 |
40 | Apache License, Version 2.0
41 | http://www.apache.org/licenses/LICENSE-2.0.txt
42 | repo
43 |
44 |
45 |
46 |
47 |
48 |
49 | geeks
50 | Fabric8 Development Team
51 | fabric8
52 | http://fabric8.io/
53 |
54 |
55 |
56 |
57 | scm:git:git@github.com:fabric8io/mockwebserver.git
58 | scm:git:git@github.com:fabric8io/mockwebserver.git
59 | http://github.com/fabric8io/mockwebserver/
60 | ${project.version}
61 |
62 |
63 |
64 |
65 | oss-sonatype-staging
66 | Sonatype Staging Repository
67 | https://oss.sonatype.org/service/local/staging/deploy/maven2
68 |
69 |
70 |
71 |
72 | UTF-8
73 | 1.8
74 | 1.8
75 |
76 |
77 | 3.12.12
78 | 2.13.0
79 | 0.3.0
80 |
81 |
82 | 2.0-groovy-3.0
83 | 1.13.0
84 |
85 |
86 |
87 |
88 | com.squareup.okhttp3
89 | mockwebserver
90 | ${okhttp.version}
91 |
92 |
93 |
94 | com.fasterxml.jackson.core
95 | jackson-databind
96 | ${jackson.version}
97 |
98 |
99 |
100 | io.fabric8
101 | zjsonpatch
102 | ${zjsonpatch.version}
103 |
104 |
105 |
106 | org.spockframework
107 | spock-core
108 | ${spock.version}
109 | test
110 |
111 |
112 |
113 |
114 |
115 |
116 | org.apache.maven.plugins
117 | maven-compiler-plugin
118 | 3.8.1
119 |
120 |
121 | org.apache.maven.plugins
122 | maven-jar-plugin
123 | 3.2.0
124 |
125 |
126 | org.apache.maven.plugins
127 | maven-surefire-plugin
128 | true
129 | 2.22.2
130 |
131 |
132 | org.apache.maven.plugins
133 | maven-gpg-plugin
134 | 1.6
135 |
136 | true
137 |
138 |
139 |
140 | com.mycila
141 | license-maven-plugin
142 | 2.11
143 |
144 | true
145 | header.txt
146 |
147 | Red Hat, Inc.
148 |
149 |
150 | .editorconfig
151 | license.txt
152 | **/test-kubeconfig
153 |
154 |
155 |
156 |
157 | org.codehaus.gmavenplus
158 | gmavenplus-plugin
159 | ${gmavenplus-plugin.version}
160 | true
161 |
162 |
163 |
164 | addSources
165 | addTestSources
166 | compileTests
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 | release
177 |
178 |
179 |
180 | org.apache.maven.plugins
181 | maven-gpg-plugin
182 |
183 |
184 | sign-artifacts
185 | verify
186 |
187 | sign
188 |
189 |
190 |
191 |
192 |
193 | org.apache.maven.plugins
194 | maven-enforcer-plugin
195 | 1.4.1
196 |
197 |
198 | enforce-no-snapshots
199 |
200 | enforce
201 |
202 |
203 |
204 |
205 | No Snapshots Allowed!
206 |
207 |
208 | false
209 |
210 |
211 |
212 |
213 |
214 | org.apache.maven.plugins
215 | maven-javadoc-plugin
216 | 3.2.0
217 |
218 | ${javadoc.include.deps}
219 |
220 | ${javadoc.source.includes}
221 |
222 | ${javadoc.package.excludes}
223 |
224 |
225 |
226 | attach-javadocs
227 |
228 | jar
229 |
230 |
231 | ${javadoc.opts}
232 |
233 |
234 |
235 |
236 |
237 | org.apache.maven.plugins
238 | maven-source-plugin
239 | 3.2.1
240 |
241 |
242 | attach-sources
243 |
244 | jar
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 | doclint-java8-disable
254 |
255 | [1.8,)
256 |
257 |
258 | -Xdoclint:none
259 |
260 |
261 |
262 |
263 |
264 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | Mock Web Server
2 | ---------------
3 |
4 |
5 |
6 | > [!IMPORTANT]
7 | > This repository has been archived.
8 | >
9 | > The content of this project has been moved to the [Fabric8 Kubernetes Client](https://github.com/fabric8io/kubernetes-client) repository.
10 | > The Maven coordinates remain the same `io.fabric8:mockwebserver`, except for the version which is now aligned with the Kubernetes Client version.
11 | >
12 | > See https://github.com/fabric8io/kubernetes-client/issues/5430 for more information.
13 |
14 |
15 |
16 | [](https://circleci.com/gh/fabric8io/mockwebserver)
17 | [](https://maven-badges.herokuapp.com/maven-central/io.fabric8/mockwebserver/)
18 |
19 |
20 | This is a DSL wrapper around the ``okhttp`` [mockwebserver](https://github.com/square/okhttp/tree/master/mockwebserver), that intends to make it easier to use.
21 |
22 | **Features**:
23 |
24 | - [EasyMock](http://easymock.org)-like DSL *(expect / respond / frequency)*
25 | - Supports most HTTP operations
26 | - Supports Chunked responses
27 | - Supports WebSockets
28 | - Supports String or Object bodies (which are serialized to JSON or YAML).
29 | - Supports SSL
30 | - CRUD mocking
31 |
32 | ### Creating a Mock Web Server
33 |
34 | To create an instance of the Mock Web Server:
35 |
36 | DefaultMockServer server = new DefaultMockServer();
37 | server.start();
38 |
39 | The ``DefaultMockServer`` instance wraps around ``okhttp3.mockwebserver.MockWebServer``, and delegates lifecycle and feedback calls to it **(see below)** and on top of that provides methods for [Setting expectations][]
40 |
41 | When the start method is invoked a random port will be bound on localhost and the Mock Web Server will listen on that port. The hostname, port or URL of the server are available:
42 |
43 | String hostname = server.getHostName();
44 | int port = server.getPort();
45 | String url = server.url("/api/v1/users");
46 |
47 | #### Creating a request
48 |
49 | To create a request using the ``okhttp`` you just need to obtain the url from Mock WebServer instance and use it in your request
50 |
51 | OkHttpClient client = new OkHttpClient();
52 | Request request = new Request.Builder().url(server.url("/api/v1/users")).get().build();
53 | Response response = client.newCall(request).execute();
54 |
55 | To cleanup after using the Mock Web Server:
56 |
57 | server.shutdown();
58 |
59 | ### Setting expectations ###
60 |
61 | The Mock Web Server provides a DSL for setting expectations. The entry point to the DSL is the ``expect()`` method.
62 |
63 | For example:
64 |
65 | server.expect().withPath("/api/v1/users").andReturn(200, "admin").once();
66 |
67 | With the code above, we tell the Mock Web Server that the first request send to ``/api/v1/users`` should return ``admin`` with HTTP status code 200.
68 | Follow up request will result in Http status code 404 **(not found)**.
69 |
70 | If instead of just once you would like the expected behavior multiple times you can use ``times(int number)``:
71 |
72 | server.expect().withPath("/api/v1/users").andReturn(200, "admin").times(2);
73 |
74 | And if after the first two times you would like to change the response:
75 |
76 | server.expect().withPath("/api/v1/users").andReturn(200, "admin").times(2);
77 | server.expect().withPath("/api/v1/users").andReturn(200, "admin root").once();
78 |
79 | To set an infinite number of times you can use ``always()``.
80 |
81 | server.expect().withPath("/api/v1/users").andReturn(200, "admin").always();
82 |
83 |
84 | ### Serializing response bodies ###
85 |
86 | All the snippets above are using String responses. For complex JSON or Yaml objects handcrafting Strings can be tedious. So the DSL also supports passing objects which will be serialized to JSON / YAML.
87 |
88 | In this example we are going to use the [User class](src/test/groovy/io/fabric8/mockwebserver/User.groovy) and pass an instance of it to the Mock Web Server:
89 |
90 | server.expect().get().withPath("/api/v1/users").andReturn(200, new User(1L, "admin", "true").always();
91 | Request request = new Request.Builder().url(server.url("/api/v1/users")).get().build();
92 | Response response = client.newCall(request).execute();
93 |
94 | In this case the response boody should looks like this:
95 |
96 | {
97 | "id": 1,
98 | "username": "admin",
99 | "enabled": true
100 | }
101 |
102 | ### Using WebSockets ###
103 |
104 | To support mock of web sockets this wrapper allows you to either specify a ``request/response`` style or define a sequence of messages and the time each message will be fired.
105 |
106 | #### Using Request Response style ####
107 |
108 | server.expect().get().withPath("/api/v1/users/shell")
109 | .andUpgradeToWebSocket()
110 | .open()
111 | .expect("create root").andEmit("CREATED").once()
112 | .expect("delete root").andEmit("DELETED").once()
113 | .done()
114 | .once()
115 |
116 | #### Emitting timed Web Socket messages ####
117 |
118 | server.expect().withPath("/api/v1/users/watch")
119 | .andUpgradeToWebSocket()
120 | .open()
121 | .waitFor(1000).andEmit("root - CREATED")
122 | .waitFor(1500).andEmit("root - DELETED")
123 | .done()
124 | .once()
125 |
126 | ### CRUD Mocking ###
127 |
128 | Often a rest API, will act like a CRUD (create, read, update & delete). So, it makes sense to have a different approach on setting expectations.
129 | Instead of setting expectations for every single request, to take advantage of the crud nature of the API. This means that a get operation will return the resource, that has been previously created.
130 | In the same spirit a delete or update request, will act on a previously created resource and so on.
131 |
132 | To use CRUD mocking, the user will have to implement two interfaces:
133 |
134 | - An AttributeExtractor
135 | - A ResponseComposer
136 |
137 | The AttributeExtractor extracts attributes, from the path of the request and from the actual resource. Attributes is what is used to match paths to resources.
138 | In order to have a successful match, the path attributes need to be a subset of the actual resource attributes:
139 | The actual methods that need to be implemented are:
140 |
141 | AttributeSet extract(String path);
142 | AttributeSet extract(T object);
143 |
144 |
145 | Each get request, may result in one or more resources (based on the attributes, as explained above). To compose multiple resources into a single response, the ResponseComposer comes into play.
146 | The ResponseComposer is a simple object that specifies how multiple Strings can be composed into a single one:
147 |
148 | String compose(Collection items);
149 |
150 |
151 | #### More Examples ####
152 |
153 | This wrapper has been extensively used at:
154 |
155 | - [Kubernetes Client](https://github.com/fabric8io/kubernetes-client)
156 | - [Docker Client](https://github.com/fabric8io/docker-client)
157 |
--------------------------------------------------------------------------------
/release.groovy:
--------------------------------------------------------------------------------
1 | #!/usr/bin/groovy
2 | /**
3 | * Copyright (C) 2015 Red Hat, Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | def updateDependencies(source){
18 |
19 | def properties = []
20 |
21 | updatePropertyVersion{
22 | updates = properties
23 | repository = source
24 | project = 'fabric8io/mockwebserver'
25 | }
26 | }
27 |
28 | def stage(){
29 | return stageProject{
30 | project = 'fabric8io/mockwebserver'
31 | useGitTagForNextVersion = true
32 | }
33 | }
34 |
35 | def approveRelease(project){
36 | def releaseVersion = project[1]
37 | approve{
38 | room = null
39 | version = releaseVersion
40 | console = null
41 | environment = 'fabric8'
42 | }
43 | }
44 |
45 | def release(project){
46 | releaseProject{
47 | stagedProject = project
48 | useGitTagForNextVersion = true
49 | helmPush = false
50 | groupId = 'io.fabric8'
51 | githubOrganisation = 'fabric8io'
52 | artifactIdToWatchInCentral = 'mockwebserver'
53 | artifactExtensionToWatchInCentral = 'jar'
54 | }
55 | }
56 |
57 | def mergePullRequest(prId){
58 | mergeAndWaitForPullRequest{
59 | project = 'fabric8io/mockwebserver'
60 | pullRequestId = prId
61 | }
62 |
63 | }
64 | return this;
65 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/Context.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver;
17 |
18 | import com.fasterxml.jackson.databind.ObjectMapper;
19 |
20 | public class Context {
21 |
22 | private final ObjectMapper mapper;
23 |
24 | public Context() {
25 | this(new ObjectMapper());
26 | }
27 |
28 | public Context(ObjectMapper mapper) {
29 | this.mapper = mapper;
30 | }
31 |
32 | public ObjectMapper getMapper() {
33 | return mapper;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/DefaultMockServer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver;
18 |
19 | import io.fabric8.mockwebserver.dsl.MockServerExpectation;
20 | import io.fabric8.mockwebserver.internal.MockDispatcher;
21 | import io.fabric8.mockwebserver.internal.MockSSLContextFactory;
22 | import io.fabric8.mockwebserver.internal.MockServerExpectationImpl;
23 | import okhttp3.mockwebserver.Dispatcher;
24 | import okhttp3.mockwebserver.MockWebServer;
25 | import okhttp3.mockwebserver.RecordedRequest;
26 |
27 | import java.io.IOException;
28 | import java.net.InetAddress;
29 | import java.net.Proxy;
30 | import java.util.HashMap;
31 | import java.util.Map;
32 | import java.util.Queue;
33 | import java.util.concurrent.TimeUnit;
34 | import java.util.concurrent.atomic.AtomicBoolean;
35 | import java.util.concurrent.atomic.AtomicInteger;
36 | import java.util.concurrent.atomic.AtomicReference;
37 |
38 | public class DefaultMockServer implements MockServer {
39 |
40 | private final Context context;
41 | private final boolean useHttps;
42 | private final MockWebServer server;
43 | private final Map> responses;
44 | private final AtomicInteger lastRequestCount;
45 | private final AtomicReference lastRequest;
46 |
47 | private final AtomicBoolean initialized = new AtomicBoolean();
48 | private final AtomicBoolean shutdown = new AtomicBoolean();
49 |
50 | public DefaultMockServer() {
51 | this(new Context(), new MockWebServer(), new HashMap<>(), false);
52 | }
53 |
54 | public DefaultMockServer(boolean useHttps) {
55 | this(new Context(), new MockWebServer(), new HashMap<>(), useHttps);
56 | }
57 |
58 | public DefaultMockServer(MockWebServer server, Map> responses, boolean useHttps) {
59 | this(new Context(), server, responses, useHttps);
60 | }
61 |
62 | public DefaultMockServer(Context context, MockWebServer server, Map> responses, boolean useHttps) {
63 | this(context, server, responses, new MockDispatcher(responses), useHttps);
64 | }
65 |
66 | public DefaultMockServer(Context context, MockWebServer server, Map> responses, Dispatcher dispatcher, boolean useHttps) {
67 | this.context = context;
68 | this.useHttps = useHttps;
69 | this.server = server;
70 | this.responses = responses;
71 | this.lastRequest = new AtomicReference<>();
72 | this.lastRequestCount = new AtomicInteger(0);
73 | this.server.setDispatcher(dispatcher);
74 | }
75 |
76 |
77 | private void startInternal() {
78 | if (initialized.compareAndSet(false, true)) {
79 | if (useHttps) {
80 | server.useHttps(MockSSLContextFactory.create().getSocketFactory(), false);
81 | }
82 | onStart();
83 | }
84 | }
85 |
86 | private void shutdownInternal() {
87 | if (shutdown.compareAndSet(false, true)) {
88 | onShutdown();
89 | }
90 | }
91 |
92 | public final void start() {
93 | try {
94 | startInternal();
95 | server.start();
96 | } catch (IOException e) {
97 | throw new MockServerException("Exception when starting DefaultMockServer", e);
98 | }
99 | }
100 |
101 | public final void start(int port) {
102 | try {
103 | startInternal();
104 | server.start(port);
105 | } catch (IOException e) {
106 | throw new MockServerException("Exception when starting DefaultMockServer with port", e);
107 | }
108 | }
109 |
110 | public final void start(InetAddress inetAddress, int port) {
111 | try {
112 | startInternal();
113 | server.start(inetAddress, port);
114 | } catch (IOException e) {
115 | throw new MockServerException("Exception when starting DefaultMockServer with InetAddress and port", e);
116 | }
117 | }
118 |
119 | public final void shutdown() {
120 | try {
121 | server.shutdown();
122 | } catch (IOException e) {
123 | throw new MockServerException("Exception when stopping DefaultMockServer", e);
124 | } finally {
125 | shutdownInternal();
126 | }
127 | }
128 |
129 | /**
130 | * {@inheritDoc}
131 | */
132 | @Override
133 | public String url(String path) {
134 | return server.url(path).toString();
135 | }
136 |
137 | /**
138 | * {@inheritDoc}
139 | */
140 | @Override
141 | public int getPort() {
142 | return server.getPort();
143 | }
144 |
145 | /**
146 | * {@inheritDoc}
147 | */
148 | @Override
149 | public String getHostName() {
150 | return server.getHostName();
151 | }
152 |
153 | /**
154 | * {@inheritDoc}
155 | */
156 | @Override
157 | public Proxy toProxyAddress() {
158 | return server.toProxyAddress();
159 | }
160 | /**
161 | * {@inheritDoc}
162 | */
163 | @Override
164 | public MockServerExpectation expect() {
165 | return new MockServerExpectationImpl(responses, context);
166 | }
167 |
168 | /**
169 | * {@inheritDoc}
170 | */
171 | @Override
172 | public int getRequestCount() {
173 | return server.getRequestCount();
174 | }
175 |
176 | /**
177 | * {@inheritDoc}
178 | */
179 | @Override
180 | public RecordedRequest takeRequest() throws InterruptedException {
181 | return server.takeRequest();
182 | }
183 |
184 | /**
185 | * {@inheritDoc}
186 | */
187 | @Override
188 | public RecordedRequest takeRequest(long timeout, TimeUnit unit) throws InterruptedException {
189 | return server.takeRequest(timeout, unit);
190 | }
191 |
192 | /**
193 | * {@inheritDoc}
194 | */
195 | @Override
196 | public synchronized RecordedRequest getLastRequest() throws InterruptedException {
197 | if (lastRequest.get() != null && getRequestCount() == lastRequestCount.get()) {
198 | return lastRequest.get();
199 | }
200 | int requestCount = getRequestCount() - lastRequestCount.getAndSet(getRequestCount());
201 | RecordedRequest latestRequest = null;
202 | while (requestCount-- > 0) {
203 | latestRequest = takeRequest();
204 | }
205 | lastRequest.set(latestRequest);
206 | return latestRequest;
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/MockServer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver;
17 |
18 | import io.fabric8.mockwebserver.dsl.MockServerExpectation;
19 | import okhttp3.mockwebserver.RecordedRequest;
20 |
21 | import java.net.Proxy;
22 | import java.util.concurrent.TimeUnit;
23 |
24 | public interface MockServer {
25 |
26 | /**
27 | * This method is called right before start. Override it to add extra initialization.
28 | */
29 | default void onStart() {
30 | }
31 |
32 | /**
33 | * This method is called right after shutdown. Override it to add extra cleanup.
34 | */
35 | default void onShutdown() {
36 | }
37 |
38 | /**
39 | * The port for the {@link okhttp3.mockwebserver.MockWebServer}.
40 | *
41 | * @return the MockWebServer port.
42 | */
43 | int getPort();
44 |
45 | /**
46 | * The host name for the {@link okhttp3.mockwebserver.MockWebServer}.
47 | * @return the MockWebServer host name;
48 | */
49 | String getHostName();
50 |
51 | /**
52 | * Returns a {@link Proxy} for the {@link okhttp3.mockwebserver.MockWebServer} with the current HostName and Port.
53 | *
54 | * @return a Proxy for the MockWebServer.
55 | */
56 | Proxy toProxyAddress();
57 |
58 | /**
59 | * Returns a String URL for connecting to this server.
60 | *
61 | * @param path the request path, such as "/".
62 | */
63 | String url(String path);
64 |
65 | /**
66 | * Returns a {@link MockServerExpectation} to set the expectations.
67 | *
68 | * @return the MockServerExpectation builder.
69 | */
70 | MockServerExpectation expect();
71 |
72 | /**
73 | * Returns the number of HTTP requests received thus far by this server. This may exceed the
74 | * number of HTTP connections when connection reuse is in practice.
75 | */
76 | int getRequestCount();
77 |
78 | /**
79 | * Awaits the next HTTP request, removes it, and returns it. Callers should use this to verify the
80 | * request was sent as intended. This method will block until the request is available, possibly
81 | * forever.
82 | *
83 | * @return the head of the request queue
84 | */
85 | RecordedRequest takeRequest() throws InterruptedException;
86 |
87 | /**
88 | * Awaits the next HTTP request (waiting up to the specified wait time if necessary), removes it,
89 | * and returns it. Callers should use this to verify the request was sent as intended within the
90 | * given time.
91 | *
92 | * @param timeout how long to wait before giving up, in units of {@code unit}
93 | * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
94 | * @return the head of the request queue
95 | */
96 | RecordedRequest takeRequest(long timeout, TimeUnit unit) throws InterruptedException;
97 |
98 | /**
99 | * Returns the last (most recent) HTTP request processed by the {@link okhttp3.mockwebserver.MockWebServer}.
100 | *
101 | * n.b. This method clears the request queue.
102 | *
103 | * @return the most recent RecordedRequest or null if none was processed.
104 | */
105 | RecordedRequest getLastRequest() throws InterruptedException;
106 | }
107 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/MockServerException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver;
17 |
18 | public class MockServerException extends RuntimeException {
19 |
20 | private static final long serialVersionUID = 2158577731194403856L;
21 |
22 | public MockServerException(String message) {
23 | super(message);
24 | }
25 |
26 | public MockServerException(String message, Throwable cause) {
27 | super(message, cause);
28 | }
29 |
30 | /**
31 | * Wraps the provided {@link Throwable} in a MockServerException in case it's checked exception.
32 | *
33 | *
For RuntimeException instances, the original exception is returned.
34 | *
35 | * @param cause Throwable to wrap.
36 | * @return the original exception in case it's unchecked, or a MockServerException wrapping it.
37 | */
38 | public static RuntimeException launderThrowable(Throwable cause) {
39 | return launderThrowable("An error has occurred.", cause);
40 | }
41 |
42 | /**
43 | * Wraps the provided {@link Throwable} in a MockServerException in case it's checked exception.
44 | *
45 | *
For RuntimeException instances, the original exception is returned.
46 | *
47 | * @param message Message to use for the exception.
48 | * @param cause Throwable to wrap.
49 | * @return the original exception in case it's unchecked, or a MockServerException wrapping it.
50 | */
51 | public static RuntimeException launderThrowable(String message, Throwable cause) {
52 | if (cause instanceof RuntimeException) {
53 | return (RuntimeException) cause;
54 | } else if (cause instanceof Error) {
55 | throw (Error) cause;
56 | } else if (cause instanceof InterruptedException) {
57 | Thread.currentThread().interrupt();
58 | }
59 | throw new MockServerException(message , cause);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/ServerRequest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver;
17 |
18 | public interface ServerRequest {
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/ServerResponse.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver;
17 |
18 | import okhttp3.mockwebserver.MockResponse;
19 | import okhttp3.mockwebserver.RecordedRequest;
20 |
21 | public interface ServerResponse {
22 |
23 | boolean isRepeatable();
24 |
25 | MockResponse toMockResponse(RecordedRequest recordedRequest);
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/Attribute.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.Objects;
21 | import java.util.stream.Collectors;
22 |
23 | import static io.fabric8.mockwebserver.crud.AttributeType.WITH;
24 |
25 | public class Attribute {
26 |
27 | private final Key key;
28 | private final List values;
29 | private final AttributeType type;
30 |
31 |
32 | public Attribute(Key key, List values, AttributeType type) {
33 | this.key = key;
34 | this.values = values;
35 | this.type = type;
36 | }
37 |
38 | public Attribute(Key key, Value value, AttributeType type) {
39 | this(key, Collections.singletonList(value), type);
40 | }
41 |
42 | public Attribute(String key, String value, AttributeType type) {
43 | this(new Key(key), new Value(value), type);
44 | }
45 |
46 | public Attribute(String key, List values, AttributeType type) {
47 | this(new Key(key), values.stream().map(Value::new).collect(Collectors.toList()), type);
48 | }
49 |
50 | public Attribute(Key key, Value value) {
51 | this(key,value,WITH);
52 | }
53 |
54 | public Attribute(String key, String value) {
55 | this(new Key(key), new Value(value));
56 | }
57 |
58 | public Key getKey() {
59 | return key;
60 | }
61 |
62 | public List getValues() {
63 | return values;
64 | }
65 |
66 | @Override
67 | public boolean equals(Object o) {
68 | if (this == o) return true;
69 | if (o == null || getClass() != o.getClass()) return false;
70 | Attribute attribute = (Attribute) o;
71 | return Objects.equals(key, attribute.key) && Objects.equals(values, attribute.values);
72 | }
73 |
74 | @Override
75 | public int hashCode() {
76 | return Objects.hash(key, values);
77 | }
78 |
79 | @Override
80 | public String toString() {
81 | return "{" +
82 | "key:" + key +
83 | ", values:" + values +
84 | '}';
85 | }
86 |
87 | public AttributeType getType() {
88 | return type;
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/AttributeExtractor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | public interface AttributeExtractor {
19 |
20 | AttributeSet fromPath(String path);
21 |
22 | AttributeSet fromResource(String resource);
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/AttributeSet.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | import java.util.Arrays;
19 | import java.util.Collection;
20 | import java.util.HashMap;
21 | import java.util.Map;
22 | import java.util.Objects;
23 |
24 | public class AttributeSet {
25 |
26 | // Package-private for testing
27 | final Map attributes;
28 |
29 | public static AttributeSet merge(AttributeSet... attributeSets) {
30 | Map all = new HashMap<>();
31 | if (attributeSets != null) {
32 | for (AttributeSet f : attributeSets) {
33 | if (f != null && f.attributes != null) {
34 | all.putAll(f.attributes);
35 | }
36 | }
37 | }
38 | return new AttributeSet(all);
39 | }
40 |
41 | public static AttributeSet map(Attribute... attributes) {
42 | Map all = new HashMap<>();
43 | if (attributes != null) {
44 | for (Attribute a : attributes) {
45 | all.put(a.getKey(), a);
46 | }
47 | }
48 | return new AttributeSet(all);
49 | }
50 |
51 | public AttributeSet(Attribute... attributes) {
52 | this(Arrays.asList(attributes));
53 | }
54 |
55 | public AttributeSet(Collection attributes) {
56 | this(AttributeSet.map(attributes.toArray(new Attribute[0])).attributes);
57 | }
58 |
59 | public AttributeSet(Map attributes) {
60 | this.attributes = attributes;
61 | }
62 |
63 | public AttributeSet add(Attribute... attr) {
64 | Map all = new HashMap<>(attributes);
65 | for(Attribute a : attr) {
66 | all.put(a.getKey(), a);
67 | }
68 | return new AttributeSet(all);
69 | }
70 |
71 | public boolean containsKey(String key) {
72 | return containsKey(new Key(key));
73 | }
74 |
75 | public boolean containsKey(Key key) {
76 | return attributes.containsKey(key);
77 | }
78 |
79 | /**
80 | * matches if attributes in db has (or doesn't if WITHOUT command) a set of candidate attributes
81 | * Also supports EXISTS and NOT_EXISTS operations
82 | * @param candidate - set of candidate attributes
83 | * @return match
84 | */
85 | public boolean matches(AttributeSet candidate) {
86 | return candidate.attributes.values()
87 | .stream()
88 | .allMatch(this::satisfiesAttribute);
89 | }
90 |
91 | private boolean satisfiesAttribute(Attribute c) {
92 | switch (c.getType()) {
93 | case EXISTS:
94 | return attributes.containsKey(c.getKey());
95 | case NOT_EXISTS:
96 | return !attributes.containsKey(c.getKey());
97 | case IN: {
98 | if (attributes.containsKey(c.getKey())) {
99 | if (attributes.get(c.getKey()).getValues().size() > 1) {
100 | throw new IllegalArgumentException("Attribute " + c.getKey() + " has multiple values, can't use IN operation");
101 | }
102 | return c.getValues().contains(attributes.get(c.getKey()).getValues().iterator().next());
103 | }
104 | return false;
105 | }
106 | case NOT_IN: {
107 | if (attributes.containsKey(c.getKey())) {
108 | if (attributes.get(c.getKey()).getValues().size() > 1) {
109 | throw new IllegalArgumentException("Attribute " + c.getKey() + " has multiple values, can't use NOT_IN operation");
110 | }
111 | return !c.getValues().contains(attributes.get(c.getKey()).getValues().iterator().next());
112 | }
113 | return true;
114 | }
115 | case WITHOUT:
116 | return !attributes.containsValue(c);
117 | case WITH:
118 | default:
119 | return attributes.containsValue(c);
120 | }
121 | }
122 |
123 | @Override
124 | public boolean equals(Object o) {
125 | if (this == o) return true;
126 | if (o == null || getClass() != o.getClass()) return false;
127 | AttributeSet that = (AttributeSet) o;
128 | return Objects.equals(attributes, that.attributes);
129 | }
130 |
131 | @Override
132 | public int hashCode() {
133 | return Objects.hash(attributes);
134 | }
135 |
136 | public Attribute getAttribute(String key) {
137 | return attributes.get(new Key(key));
138 | }
139 |
140 | @Override
141 | public String toString() {
142 | return "{" +
143 | "attributes: " + attributes +
144 | '}';
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/AttributeType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | public enum AttributeType {
19 | WITH, WITHOUT, EXISTS, NOT_EXISTS, IN, NOT_IN
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/CrudDispatcher.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | import com.fasterxml.jackson.databind.JsonNode;
19 | import io.fabric8.mockwebserver.Context;
20 | import io.fabric8.mockwebserver.MockServerException;
21 | import io.fabric8.zjsonpatch.JsonPatch;
22 | import okhttp3.mockwebserver.Dispatcher;
23 | import okhttp3.mockwebserver.MockResponse;
24 | import okhttp3.mockwebserver.RecordedRequest;
25 |
26 | import java.net.HttpURLConnection;
27 | import java.util.ArrayList;
28 | import java.util.Collections;
29 | import java.util.LinkedHashMap;
30 | import java.util.List;
31 | import java.util.Map;
32 |
33 | public class CrudDispatcher extends Dispatcher {
34 |
35 | private static final String POST = "POST";
36 | private static final String PUT = "PUT";
37 | private static final String PATCH = "PATCH";
38 | private static final String GET = "GET";
39 | private static final String DELETE = "DELETE";
40 |
41 | protected final Map map = Collections.synchronizedMap(new LinkedHashMap<>());
42 |
43 | protected final Context context;
44 | protected final AttributeExtractor attributeExtractor;
45 | protected final ResponseComposer responseComposer;
46 |
47 | public CrudDispatcher(Context context, AttributeExtractor attributeExtractor, ResponseComposer responseComposer) {
48 | this.context = context;
49 | this.attributeExtractor = attributeExtractor;
50 | this.responseComposer = responseComposer;
51 | }
52 |
53 | @Override
54 | public MockResponse dispatch(RecordedRequest request) {
55 | String path = request.getPath();
56 | switch (request.getMethod().toUpperCase()) {
57 | case POST:
58 | return handleCreate(request);
59 | case PUT:
60 | return handleUpdate(request);
61 | case PATCH:
62 | return handlePatch(request);
63 | case GET:
64 | return handleGet(path);
65 | case DELETE:
66 | return handleDelete(path);
67 | default:
68 | return null;
69 | }
70 | }
71 |
72 | public MockResponse handleCreate(RecordedRequest request) {
73 | return handleCreate(request.getPath(), request.getBody().readUtf8());
74 | }
75 |
76 | /**
77 | * Adds the specified object to the in-memory db.
78 | *
79 | * @param path for the request.
80 | * @param body Request body as String (UTF-8).
81 | * @return a MockResponse to be dispatched.
82 | */
83 | public MockResponse handleCreate(String path, String body) {
84 | MockResponse response = new MockResponse();
85 | AttributeSet features = AttributeSet.merge(attributeExtractor.fromPath(path), attributeExtractor.fromResource(body));
86 | synchronized (map) {
87 | map.put(features, body);
88 | }
89 | response.setBody(body);
90 | response.setResponseCode(202);
91 | return response;
92 | }
93 |
94 | public MockResponse handlePatch(RecordedRequest request) {
95 | return handlePatch(request.getPath(), request.getBody().readUtf8());
96 | }
97 |
98 | /**
99 | * Patches the specified object to the in-memory db.
100 | *
101 | * @param path for the request.
102 | * @param body Request body as String (UTF-8).
103 | * @return a MockResponse to be dispatched.
104 | */
105 | public MockResponse handlePatch(String path, String body) {
106 | MockResponse response = new MockResponse();
107 | String existingObjectBody = doGet(path);
108 | if (existingObjectBody == null) {
109 | response.setResponseCode(404);
110 | } else {
111 | try {
112 | JsonNode patch = context.getMapper().readTree(body);
113 | JsonNode source = context.getMapper().readTree(existingObjectBody);
114 | JsonNode updated = JsonPatch.apply(patch, source);
115 | String updatedAsString = context.getMapper().writeValueAsString(updated);
116 | AttributeSet features = AttributeSet.merge(attributeExtractor.fromPath(path),
117 | attributeExtractor.fromResource(updatedAsString));
118 | synchronized (map) {
119 | map.put(features, updatedAsString);
120 | }
121 | response.setResponseCode(202);
122 | response.setBody(updatedAsString);
123 | } catch (Exception e) {
124 | throw new MockServerException("Exception when handling CRUD patch", e);
125 | }
126 |
127 | }
128 | return response;
129 | }
130 |
131 | public MockResponse handleUpdate(RecordedRequest request) {
132 | return handleUpdate(request.getPath(), request.getBody().readUtf8());
133 | }
134 |
135 | /**
136 | * Updates the specified object to the in-memory db.
137 | *
138 | * @param path for the request.
139 | * @param body Request body as String (UTF-8).
140 | * @return a MockResponse to be dispatched.
141 | */
142 | public MockResponse handleUpdate(String path, String body) {
143 | final String currentItem = doGet(path);
144 | final MockResponse response = handleCreate(path, body);
145 | if (currentItem == null) {
146 | response.setResponseCode(HttpURLConnection.HTTP_CREATED);
147 | }
148 | return response;
149 | }
150 |
151 | /**
152 | * Performs a get for the corresponding object from the in-memory db.
153 | *
154 | * @param path for the request.
155 | * @return a MockResponse to be dispatched.
156 | */
157 | public MockResponse handleGet(String path) {
158 | MockResponse response = new MockResponse();
159 |
160 | String body = doGet(path);
161 | if (body == null) {
162 | response.setResponseCode(404);
163 | } else {
164 | response.setResponseCode(200);
165 | response.setBody(body);
166 | }
167 | return response;
168 | }
169 |
170 |
171 | /**
172 | * Performs a delete for the corresponding object from the in-memory db.
173 | *
174 | * @param path for the request.
175 | * @return a MockResponse to be dispatched.
176 | */
177 | public MockResponse handleDelete(String path) {
178 | MockResponse response = new MockResponse();
179 | List items = new ArrayList<>();
180 | AttributeSet query = attributeExtractor.fromPath(path);
181 |
182 | synchronized (map) {
183 | for (Map.Entry entry : map.entrySet()) {
184 | if (entry.getKey().matches(query)) {
185 | items.add(entry.getKey());
186 | }
187 | }
188 | if (!items.isEmpty()) {
189 | for (AttributeSet item : items) {
190 | map.remove(item);
191 | }
192 | response.setResponseCode(200);
193 | } else {
194 | response.setResponseCode(404);
195 | }
196 | }
197 | return response;
198 | }
199 |
200 | public Map getMap() {
201 | return map;
202 | }
203 |
204 | public AttributeExtractor getAttributeExtractor() {
205 | return attributeExtractor;
206 | }
207 |
208 | public ResponseComposer getResponseComposer() {
209 | return responseComposer;
210 | }
211 |
212 |
213 | private String doGet(String path) {
214 | List items = new ArrayList<>();
215 | AttributeSet query = attributeExtractor.fromPath(path);
216 | synchronized (map) {
217 | for (Map.Entry entry : map.entrySet()) {
218 | if (entry.getKey().matches(query)) {
219 | items.add(entry.getValue());
220 | }
221 | }
222 | }
223 |
224 | if (items.isEmpty()) {
225 | return null;
226 | } else if (items.size() == 1) {
227 | return items.get(0);
228 | } else {
229 | return responseComposer.compose(items);
230 | }
231 | }
232 | }
233 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/Key.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | import java.util.Objects;
19 |
20 | public class Key {
21 |
22 | private final String name;
23 |
24 | public Key(String name) {
25 | this.name = name;
26 | }
27 |
28 | @Override
29 | public boolean equals(Object o) {
30 | if (this == o) return true;
31 | if (o == null || getClass() != o.getClass()) return false;
32 | Key key = (Key) o;
33 | return Objects.equals(name, key.name);
34 | }
35 |
36 | @Override
37 | public int hashCode() {
38 | return Objects.hash(name);
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return name;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/ResponseComposer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | import java.util.Collection;
19 |
20 | public interface ResponseComposer {
21 |
22 | String compose(Collection items);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/crud/Value.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.crud;
17 |
18 | public class Value {
19 |
20 | private static final String ANY = "*";
21 |
22 | private final String val;
23 |
24 | public Value(String value) {
25 | this.val = value;
26 | }
27 |
28 | @Override
29 | // TODO: There's a BUG here, equals({val: "*"} is true but might have different hashCode
30 | public boolean equals(Object o) {
31 | if (this == o) {
32 | return true;
33 | }
34 |
35 | if (o == null || getClass() != o.getClass()) {
36 | return false;
37 | }
38 |
39 | if (ANY.equals(val)) {
40 | return true;
41 | }
42 |
43 | Value key = (Value) o;
44 |
45 | if (ANY.equals(key.val)) {
46 | return true;
47 | }
48 | return val != null ? val.equals(key.val) : key.val == null;
49 | }
50 |
51 | @Override
52 | public int hashCode() {
53 | return val != null ? val.hashCode() : 0;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return val;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/DelayPathable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.dsl;
17 |
18 | public interface DelayPathable extends Delayable>,
19 | Pathable {
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/DelayTimesOrOnceable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.dsl;
17 |
18 | public interface DelayTimesOrOnceable extends Delayable, TimesOrOnceable {
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Delayable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.dsl;
17 |
18 | import java.util.concurrent.TimeUnit;
19 |
20 | public interface Delayable {
21 |
22 | T delay(long delay, TimeUnit delayUnit);
23 |
24 | T delay(long delayInMilliseconds);
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Doneable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Doneable {
20 | T done();
21 | }
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Emitable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Emitable {
20 |
21 | /**
22 | * Emit an event. This will be received by the client's onMessage.
23 | * @param event
24 | * @return
25 | */
26 | T andEmit(Object event);
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/EventDoneable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface EventDoneable extends Eventable>, Doneable {
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Eventable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Eventable {
20 |
21 | Emitable> expect(Object in);
22 |
23 | Emitable> expectHttpRequest(final String path);
24 |
25 | Emitable> expectSentWebSocketMessage(final Object in);
26 |
27 | Emitable waitFor(long millis);
28 |
29 | Emitable immediately();
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Failable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Failable {
20 |
21 | T failure(Object response, Exception e);
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Function.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Function {
20 | O apply(I input);
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/HttpHeaderable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface HttpHeaderable {
20 |
21 | T withHeader(String header);
22 |
23 | T withHeader(String name, String value);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/HttpMethod.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public enum HttpMethod {
20 |
21 | GET,
22 | POST,
23 | PUT,
24 | PATCH,
25 | DELETE,
26 | OPTIONS,
27 | CONNECT,
28 | ANY
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/HttpMethodable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface HttpMethodable {
20 |
21 | T any();
22 |
23 | T post();
24 |
25 | T get();
26 |
27 | T put();
28 |
29 | T delete();
30 |
31 | T patch();
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/HttpStatusable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface HttpStatusable {
20 |
21 | T withStatus(int statusCode);
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/MockServerExpectation.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.dsl;
17 |
18 | public interface MockServerExpectation extends HttpMethodable>>>,
19 | DelayPathable>>,
20 | ReturnOrWebsocketable>,
21 | TimesOnceableOrHttpHeaderable {
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Onceable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 io.fabric8.mockwebserver.dsl;
17 |
18 | public interface Onceable {
19 |
20 | T once();
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Openable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Openable {
20 |
21 | T open(Object... response);
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Pathable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | public interface Pathable {
20 |
21 | T withPath(String path);
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/io/fabric8/mockwebserver/dsl/Replyable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Red Hat, Inc.
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 |
17 | package io.fabric8.mockwebserver.dsl;
18 |
19 | import java.util.List;
20 |
21 | import io.fabric8.mockwebserver.utils.BodyProvider;
22 | import io.fabric8.mockwebserver.utils.ResponseProvider;
23 |
24 | public interface Replyable {
25 |
26 | T andReply(int statusCode, BodyProvider