mimicker 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mimicker-0.1.0/LICENSE +201 -0
- mimicker-0.1.0/PKG-INFO +235 -0
- mimicker-0.1.0/README.md +212 -0
- mimicker-0.1.0/mimicker/__init__.py +0 -0
- mimicker-0.1.0/mimicker/handler.py +79 -0
- mimicker-0.1.0/mimicker/mimicker.py +27 -0
- mimicker-0.1.0/mimicker/route.py +45 -0
- mimicker-0.1.0/mimicker/server.py +43 -0
- mimicker-0.1.0/mimicker/stub_group.py +50 -0
- mimicker-0.1.0/pyproject.toml +23 -0
mimicker-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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.
|
mimicker-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: mimicker
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight HTTP mocking server for Python
|
|
5
|
+
Home-page: https://github.com/amaziahub/mimicker
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: http,mocking,testing,mock-server,stubbing,ci-cd,http-server,testing-tools,stub-server,purepython
|
|
8
|
+
Author: Amazia Gur
|
|
9
|
+
Author-email: amaziagur@gmail.com
|
|
10
|
+
Requires-Python: >=3.7,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Requires-Dist: requests (>=2.0,<3.0)
|
|
21
|
+
Project-URL: Repository, https://github.com/amaziahub/mimicker
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
<p align="center">
|
|
25
|
+
<img src="mimicker.jpg" alt="Mimicker logo"
|
|
26
|
+
style="width: 200px; height: auto; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border: 2px solid black;">
|
|
27
|
+
</p>
|
|
28
|
+
<div>
|
|
29
|
+
|
|
30
|
+
<div align="center">
|
|
31
|
+
|
|
32
|
+
> **Mimicker** – Your lightweight, Python-native HTTP mocking server.
|
|
33
|
+
|
|
34
|
+
</div>
|
|
35
|
+
|
|
36
|
+
<div align="center">
|
|
37
|
+
|
|
38
|
+
[](https://github.com/amaziahub/mimicker/actions/workflows/test.yml)
|
|
39
|
+
[](http://doge.mit-license.org)
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
</div>
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
Mimicker is a Python-native HTTP mocking server inspired by WireMock, designed to simplify the process of stubbing and
|
|
47
|
+
mocking HTTP endpoints for testing purposes.
|
|
48
|
+
Mimicker requires no third-party libraries and is lightweight, making it ideal for integration testing, local
|
|
49
|
+
development, and CI environments.
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
- Create HTTP stubs for various endpoints and methods
|
|
54
|
+
- Mock responses with specific status codes, headers, and body content
|
|
55
|
+
- Flexible configuration for multiple endpoints
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
Mimicker can be installed directly from PyPI using pip or Poetry:
|
|
60
|
+
|
|
61
|
+
### Using pip:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install mimicker
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Using poetry:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
poetry add mimicker
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Usage
|
|
74
|
+
|
|
75
|
+
To start Mimicker on a specific port with a simple endpoint, you can use the following code snippet:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from mimicker.mimicker import mimicker, get
|
|
79
|
+
|
|
80
|
+
mimicker(8080).routes(
|
|
81
|
+
get("/hello").
|
|
82
|
+
body({"message": "Hello, World!"}).
|
|
83
|
+
status(200)
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Examples
|
|
88
|
+
|
|
89
|
+
#### Using Path Parameters
|
|
90
|
+
|
|
91
|
+
Mimicker can handle path parameters dynamically. Here's how you can mock an endpoint with a variable in the path:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from mimicker.mimicker import mimicker, get
|
|
95
|
+
|
|
96
|
+
mimicker(8080).routes(
|
|
97
|
+
get("/hello/{name}")
|
|
98
|
+
.body({"message": "Hello, {name}!"})
|
|
99
|
+
.status(200)
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
# When the client sends a request to /hello/world, the response will be:
|
|
103
|
+
# {"message": "Hello, world!"}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### Using Headers
|
|
107
|
+
|
|
108
|
+
You can also mock responses with custom headers:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from mimicker.mimicker import mimicker, get
|
|
112
|
+
|
|
113
|
+
mimicker(8080).routes(
|
|
114
|
+
get("/hello")
|
|
115
|
+
.body("Hello with headers")
|
|
116
|
+
.headers([("Content-Type", "text/plain"), ("Custom-Header", "Value")])
|
|
117
|
+
.status(200)
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
# The response will include custom headers
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Multiple Routes
|
|
124
|
+
|
|
125
|
+
Mimicker allows you to define multiple routes for different HTTP methods and paths. Here's an example with `GET`
|
|
126
|
+
and `POST` routes:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from mimicker.mimicker import mimicker, get, post
|
|
130
|
+
|
|
131
|
+
mimicker(8080).routes(
|
|
132
|
+
get("/greet")
|
|
133
|
+
.body({"message": "Hello, world!"})
|
|
134
|
+
.status(200),
|
|
135
|
+
|
|
136
|
+
post("/submit")
|
|
137
|
+
.body({"result": "Submission received"})
|
|
138
|
+
.status(201)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Now the server responds to:
|
|
142
|
+
# GET /greet -> {"message": "Hello, world!"}
|
|
143
|
+
# POST /submit -> {"result": "Submission received"}
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### Handling Different Status Codes
|
|
148
|
+
|
|
149
|
+
You can also mock different HTTP status codes for the same endpoint:
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from mimicker.mimicker import mimicker, get
|
|
153
|
+
|
|
154
|
+
mimicker(8080).routes(
|
|
155
|
+
get("/status")
|
|
156
|
+
.body({"message": "Success"})
|
|
157
|
+
.status(200),
|
|
158
|
+
|
|
159
|
+
get("/error")
|
|
160
|
+
.body({"message": "Not Found"})
|
|
161
|
+
.status(404)
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
# GET /status -> {"message": "Success"} with status 200
|
|
165
|
+
# GET /error -> {"message": "Not Found"} with status 404
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Mocking Responses with JSON Body
|
|
169
|
+
|
|
170
|
+
Mimicker supports JSON bodies, making it ideal for API testing:
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from mimicker.mimicker import mimicker, get
|
|
174
|
+
|
|
175
|
+
mimicker(8080).routes(
|
|
176
|
+
get("/json")
|
|
177
|
+
.body({"message": "Hello, JSON!"})
|
|
178
|
+
.status(200)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# The response will be: {"message": "Hello, JSON!"}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Supporting Other Body Types (Text, Files, etc.)
|
|
185
|
+
|
|
186
|
+
In addition to JSON bodies, Mimicker supports other types of content for the response body. Here's how you can return
|
|
187
|
+
text or file content:
|
|
188
|
+
|
|
189
|
+
##### Text Response:
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
from mimicker.mimicker import mimicker, get
|
|
193
|
+
|
|
194
|
+
mimicker(8080).routes(
|
|
195
|
+
get("/text")
|
|
196
|
+
.body("This is a plain text response")
|
|
197
|
+
.status(200)
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
# The response will be plain text: "This is a plain text response"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### File Response:
|
|
204
|
+
|
|
205
|
+
You can also return files from a mock endpoint:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
from mimicker.mimicker import mimicker, get
|
|
209
|
+
|
|
210
|
+
mimicker(8080).routes(
|
|
211
|
+
get("/file")
|
|
212
|
+
.body(open("example.txt", "rb").read()) # Mock a file response
|
|
213
|
+
.status(200)
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# The response will be the content of the "example.txt" file
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Available Features:
|
|
221
|
+
|
|
222
|
+
* `get(path)`: Defines a `GET` endpoint.
|
|
223
|
+
* `post(path)`: Defines a `POST` endpoint.
|
|
224
|
+
* `put(path)`: Defines a `PUT` endpoint.
|
|
225
|
+
* `delete(path)`: Defines a `DELETE` endpoint.
|
|
226
|
+
* `.body(content)`: Defines the response `body`.
|
|
227
|
+
* `.status(code)`: Defines the response `status code`.
|
|
228
|
+
* `.headers(headers)`: Defines response `headers`.
|
|
229
|
+
|
|
230
|
+
## Requirements
|
|
231
|
+
Mimicker supports Python 3.7 and above.
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
Mimicker is released under the MIT License. see the [LICENSE](LICENSE) for more information.
|
mimicker-0.1.0/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="mimicker.jpg" alt="Mimicker logo"
|
|
3
|
+
style="width: 200px; height: auto; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border: 2px solid black;">
|
|
4
|
+
</p>
|
|
5
|
+
<div>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
|
|
9
|
+
> **Mimicker** – Your lightweight, Python-native HTTP mocking server.
|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
<div align="center">
|
|
14
|
+
|
|
15
|
+
[](https://github.com/amaziahub/mimicker/actions/workflows/test.yml)
|
|
16
|
+
[](http://doge.mit-license.org)
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
</div>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
Mimicker is a Python-native HTTP mocking server inspired by WireMock, designed to simplify the process of stubbing and
|
|
24
|
+
mocking HTTP endpoints for testing purposes.
|
|
25
|
+
Mimicker requires no third-party libraries and is lightweight, making it ideal for integration testing, local
|
|
26
|
+
development, and CI environments.
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- Create HTTP stubs for various endpoints and methods
|
|
31
|
+
- Mock responses with specific status codes, headers, and body content
|
|
32
|
+
- Flexible configuration for multiple endpoints
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Mimicker can be installed directly from PyPI using pip or Poetry:
|
|
37
|
+
|
|
38
|
+
### Using pip:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install mimicker
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Using poetry:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
poetry add mimicker
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
To start Mimicker on a specific port with a simple endpoint, you can use the following code snippet:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from mimicker.mimicker import mimicker, get
|
|
56
|
+
|
|
57
|
+
mimicker(8080).routes(
|
|
58
|
+
get("/hello").
|
|
59
|
+
body({"message": "Hello, World!"}).
|
|
60
|
+
status(200)
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Examples
|
|
65
|
+
|
|
66
|
+
#### Using Path Parameters
|
|
67
|
+
|
|
68
|
+
Mimicker can handle path parameters dynamically. Here's how you can mock an endpoint with a variable in the path:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from mimicker.mimicker import mimicker, get
|
|
72
|
+
|
|
73
|
+
mimicker(8080).routes(
|
|
74
|
+
get("/hello/{name}")
|
|
75
|
+
.body({"message": "Hello, {name}!"})
|
|
76
|
+
.status(200)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# When the client sends a request to /hello/world, the response will be:
|
|
80
|
+
# {"message": "Hello, world!"}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
#### Using Headers
|
|
84
|
+
|
|
85
|
+
You can also mock responses with custom headers:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from mimicker.mimicker import mimicker, get
|
|
89
|
+
|
|
90
|
+
mimicker(8080).routes(
|
|
91
|
+
get("/hello")
|
|
92
|
+
.body("Hello with headers")
|
|
93
|
+
.headers([("Content-Type", "text/plain"), ("Custom-Header", "Value")])
|
|
94
|
+
.status(200)
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# The response will include custom headers
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### Multiple Routes
|
|
101
|
+
|
|
102
|
+
Mimicker allows you to define multiple routes for different HTTP methods and paths. Here's an example with `GET`
|
|
103
|
+
and `POST` routes:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from mimicker.mimicker import mimicker, get, post
|
|
107
|
+
|
|
108
|
+
mimicker(8080).routes(
|
|
109
|
+
get("/greet")
|
|
110
|
+
.body({"message": "Hello, world!"})
|
|
111
|
+
.status(200),
|
|
112
|
+
|
|
113
|
+
post("/submit")
|
|
114
|
+
.body({"result": "Submission received"})
|
|
115
|
+
.status(201)
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# Now the server responds to:
|
|
119
|
+
# GET /greet -> {"message": "Hello, world!"}
|
|
120
|
+
# POST /submit -> {"result": "Submission received"}
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### Handling Different Status Codes
|
|
125
|
+
|
|
126
|
+
You can also mock different HTTP status codes for the same endpoint:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from mimicker.mimicker import mimicker, get
|
|
130
|
+
|
|
131
|
+
mimicker(8080).routes(
|
|
132
|
+
get("/status")
|
|
133
|
+
.body({"message": "Success"})
|
|
134
|
+
.status(200),
|
|
135
|
+
|
|
136
|
+
get("/error")
|
|
137
|
+
.body({"message": "Not Found"})
|
|
138
|
+
.status(404)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# GET /status -> {"message": "Success"} with status 200
|
|
142
|
+
# GET /error -> {"message": "Not Found"} with status 404
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Mocking Responses with JSON Body
|
|
146
|
+
|
|
147
|
+
Mimicker supports JSON bodies, making it ideal for API testing:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from mimicker.mimicker import mimicker, get
|
|
151
|
+
|
|
152
|
+
mimicker(8080).routes(
|
|
153
|
+
get("/json")
|
|
154
|
+
.body({"message": "Hello, JSON!"})
|
|
155
|
+
.status(200)
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
# The response will be: {"message": "Hello, JSON!"}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### Supporting Other Body Types (Text, Files, etc.)
|
|
162
|
+
|
|
163
|
+
In addition to JSON bodies, Mimicker supports other types of content for the response body. Here's how you can return
|
|
164
|
+
text or file content:
|
|
165
|
+
|
|
166
|
+
##### Text Response:
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from mimicker.mimicker import mimicker, get
|
|
170
|
+
|
|
171
|
+
mimicker(8080).routes(
|
|
172
|
+
get("/text")
|
|
173
|
+
.body("This is a plain text response")
|
|
174
|
+
.status(200)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
# The response will be plain text: "This is a plain text response"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### File Response:
|
|
181
|
+
|
|
182
|
+
You can also return files from a mock endpoint:
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
from mimicker.mimicker import mimicker, get
|
|
186
|
+
|
|
187
|
+
mimicker(8080).routes(
|
|
188
|
+
get("/file")
|
|
189
|
+
.body(open("example.txt", "rb").read()) # Mock a file response
|
|
190
|
+
.status(200)
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
# The response will be the content of the "example.txt" file
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Available Features:
|
|
198
|
+
|
|
199
|
+
* `get(path)`: Defines a `GET` endpoint.
|
|
200
|
+
* `post(path)`: Defines a `POST` endpoint.
|
|
201
|
+
* `put(path)`: Defines a `PUT` endpoint.
|
|
202
|
+
* `delete(path)`: Defines a `DELETE` endpoint.
|
|
203
|
+
* `.body(content)`: Defines the response `body`.
|
|
204
|
+
* `.status(code)`: Defines the response `status code`.
|
|
205
|
+
* `.headers(headers)`: Defines response `headers`.
|
|
206
|
+
|
|
207
|
+
## Requirements
|
|
208
|
+
Mimicker supports Python 3.7 and above.
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
Mimicker is released under the MIT License. see the [LICENSE](LICENSE) for more information.
|
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import http.server
|
|
2
|
+
import json
|
|
3
|
+
from typing import Any, Tuple, Optional, Dict, Callable, List
|
|
4
|
+
|
|
5
|
+
from mimicker.stub_group import StubGroup
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MimickerHandler(http.server.SimpleHTTPRequestHandler):
|
|
9
|
+
def __init__(self, stub_matcher: StubGroup, *args, **kwargs):
|
|
10
|
+
self.stub_matcher = stub_matcher
|
|
11
|
+
super().__init__(*args, **kwargs)
|
|
12
|
+
|
|
13
|
+
def do_GET(self):
|
|
14
|
+
self._handle_request("GET")
|
|
15
|
+
|
|
16
|
+
def do_POST(self):
|
|
17
|
+
self._handle_request("POST")
|
|
18
|
+
|
|
19
|
+
def do_PUT(self):
|
|
20
|
+
self._handle_request("PUT")
|
|
21
|
+
|
|
22
|
+
def do_DELETE(self):
|
|
23
|
+
self._handle_request("DELETE")
|
|
24
|
+
|
|
25
|
+
def _handle_request(self, method: str):
|
|
26
|
+
request_headers = {key.lower(): value for key, value in self.headers.items()}
|
|
27
|
+
matched_stub, path_params = self.stub_matcher.match(
|
|
28
|
+
method, self.path, request_headers=request_headers
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if matched_stub:
|
|
32
|
+
self._send_response(matched_stub, path_params)
|
|
33
|
+
else:
|
|
34
|
+
self._send_404_response(method)
|
|
35
|
+
|
|
36
|
+
def _send_response(
|
|
37
|
+
self,
|
|
38
|
+
matched_stub: Tuple[
|
|
39
|
+
int, Any, Optional[Callable], Optional[List[Tuple[str, str]]]
|
|
40
|
+
],
|
|
41
|
+
path_params: Dict[str, str]
|
|
42
|
+
):
|
|
43
|
+
status_code, response, response_func, headers = matched_stub
|
|
44
|
+
if response_func:
|
|
45
|
+
status_code, response = response_func()
|
|
46
|
+
|
|
47
|
+
self.send_response(status_code)
|
|
48
|
+
self._set_headers(headers)
|
|
49
|
+
|
|
50
|
+
self.end_headers()
|
|
51
|
+
self._write_response(response, path_params)
|
|
52
|
+
|
|
53
|
+
def _set_headers(self, headers: Optional[List[Tuple[str, str]]]):
|
|
54
|
+
if headers:
|
|
55
|
+
for header_name, header_value in headers:
|
|
56
|
+
self.send_header(header_name, header_value)
|
|
57
|
+
if not any(header[0].lower() == 'content-type' for header in headers):
|
|
58
|
+
self.send_header('Content-Type', 'application/json')
|
|
59
|
+
else:
|
|
60
|
+
self.send_header('Content-Type', 'application/json')
|
|
61
|
+
|
|
62
|
+
def _write_response(self, response: Any, path_params: Dict[str, str]):
|
|
63
|
+
if isinstance(response, dict):
|
|
64
|
+
response = self._format_response(response, path_params)
|
|
65
|
+
self.wfile.write(json.dumps(response).encode('utf-8'))
|
|
66
|
+
elif isinstance(response, str):
|
|
67
|
+
self.wfile.write(response.encode('utf-8'))
|
|
68
|
+
else:
|
|
69
|
+
self.wfile.write(str(response).encode('utf-8'))
|
|
70
|
+
|
|
71
|
+
def _send_404_response(self, method: str):
|
|
72
|
+
self.send_response(404)
|
|
73
|
+
self.end_headers()
|
|
74
|
+
self.log_message("Responded with 404 for %s request to %s", method, self.path)
|
|
75
|
+
|
|
76
|
+
@staticmethod
|
|
77
|
+
def _format_response(response: dict, path_params: dict):
|
|
78
|
+
return {k: (v.format(**path_params) if isinstance(v, str) else v)
|
|
79
|
+
for k, v in response.items()}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from mimicker.route import Route
|
|
4
|
+
from mimicker.server import MimickerServer
|
|
5
|
+
|
|
6
|
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get(path: str) -> Route:
|
|
10
|
+
return Route("GET", path)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def post(path: str) -> Route:
|
|
14
|
+
return Route("POST", path)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def put(path: str) -> Route:
|
|
18
|
+
return Route("PUT", path)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def delete(path: str) -> Route:
|
|
22
|
+
return Route("DELETE", path)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def mimicker(port: int = 8080) -> MimickerServer:
|
|
26
|
+
server = MimickerServer(port).start()
|
|
27
|
+
return server
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from typing import Dict, Tuple, Any, Callable, Optional, Pattern, Union, List
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Route:
|
|
7
|
+
def __init__(self, method: str, path: str):
|
|
8
|
+
self.method = method
|
|
9
|
+
self.path = path
|
|
10
|
+
self._body = {}
|
|
11
|
+
self._status = 200
|
|
12
|
+
self._headers: List[Tuple[str, str]] = []
|
|
13
|
+
self._response_func: Optional[Callable[[], Tuple[int, Any]]] = None
|
|
14
|
+
|
|
15
|
+
escaped_path = re.escape(path)
|
|
16
|
+
parameterized_path = re.sub(r'\\{(\w+)\\}',
|
|
17
|
+
r'(?P<\1>[^/]+)', escaped_path)
|
|
18
|
+
self._compiled_path: Pattern = re.compile(f"^{parameterized_path}$")
|
|
19
|
+
|
|
20
|
+
def body(self, response: Union[Dict[str, Any], str] = None):
|
|
21
|
+
self._body = response if response is not None else ""
|
|
22
|
+
return self
|
|
23
|
+
|
|
24
|
+
def status(self, status_code: int):
|
|
25
|
+
self._status = status_code
|
|
26
|
+
return self
|
|
27
|
+
|
|
28
|
+
def headers(self, headers: List[Tuple[str, str]]):
|
|
29
|
+
self._headers = headers
|
|
30
|
+
return self
|
|
31
|
+
|
|
32
|
+
def response_func(self, func: Callable[[], Tuple[int, Any]]):
|
|
33
|
+
self._response_func = func
|
|
34
|
+
return self
|
|
35
|
+
|
|
36
|
+
def build(self):
|
|
37
|
+
return {
|
|
38
|
+
"method": self.method,
|
|
39
|
+
"path": self.path,
|
|
40
|
+
"compiled_path": self._compiled_path,
|
|
41
|
+
"body": self._body,
|
|
42
|
+
"status": self._status,
|
|
43
|
+
"headers": self._headers,
|
|
44
|
+
"response_func": self._response_func
|
|
45
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import atexit
|
|
2
|
+
import logging
|
|
3
|
+
import socketserver
|
|
4
|
+
import threading
|
|
5
|
+
|
|
6
|
+
from mimicker.handler import MimickerHandler
|
|
7
|
+
from mimicker.route import Route
|
|
8
|
+
from mimicker.stub_group import StubGroup
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MimickerServer:
|
|
12
|
+
def __init__(self, port: int = 8080):
|
|
13
|
+
self.stub_matcher = StubGroup()
|
|
14
|
+
self.server = socketserver.TCPServer(("", port), self._handler_factory)
|
|
15
|
+
self._thread = threading.Thread(target=self.server.serve_forever, daemon=True)
|
|
16
|
+
atexit.register(self.shutdown)
|
|
17
|
+
|
|
18
|
+
def _handler_factory(self, *args):
|
|
19
|
+
return MimickerHandler(self.stub_matcher, *args)
|
|
20
|
+
|
|
21
|
+
def routes(self, *routes: Route):
|
|
22
|
+
for route in routes:
|
|
23
|
+
route_config = route.build()
|
|
24
|
+
self.stub_matcher.add(
|
|
25
|
+
method=route_config["method"],
|
|
26
|
+
pattern=route_config["compiled_path"],
|
|
27
|
+
status_code=route_config["status"],
|
|
28
|
+
response=route_config["body"],
|
|
29
|
+
headers=route_config["headers"],
|
|
30
|
+
response_func=route_config["response_func"]
|
|
31
|
+
)
|
|
32
|
+
return self
|
|
33
|
+
|
|
34
|
+
def start(self):
|
|
35
|
+
logging.info("MimickerServer starting on port %s",
|
|
36
|
+
self.server.server_address[1])
|
|
37
|
+
self._thread.start()
|
|
38
|
+
return self
|
|
39
|
+
|
|
40
|
+
def shutdown(self):
|
|
41
|
+
self.server.shutdown()
|
|
42
|
+
if self._thread.is_alive():
|
|
43
|
+
self._thread.join()
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import Pattern, Dict, Tuple, Any, Optional, Callable, Union, List
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class StubGroup:
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.stubs: Dict[
|
|
8
|
+
str,
|
|
9
|
+
Dict[
|
|
10
|
+
Pattern,
|
|
11
|
+
Tuple[int, Any, Optional[Callable], Optional[List[Tuple[str, str]]]]
|
|
12
|
+
]
|
|
13
|
+
] = {}
|
|
14
|
+
|
|
15
|
+
def add(self, method: str, pattern: Union[str, Pattern],
|
|
16
|
+
status_code: int, response: Any,
|
|
17
|
+
response_func: Optional[Callable[[], Tuple[int, Any]]] = None,
|
|
18
|
+
headers: Optional[List[Tuple[str, str]]] = None):
|
|
19
|
+
if method not in self.stubs:
|
|
20
|
+
self.stubs[method] = {}
|
|
21
|
+
|
|
22
|
+
if isinstance(pattern, str):
|
|
23
|
+
pattern = re.compile(f"^{re.sub(r'\\{(\\w+)\\}',
|
|
24
|
+
r'(?P<\\1>[^/]+)', pattern)}$")
|
|
25
|
+
|
|
26
|
+
self.stubs[method][pattern] = (status_code, response, response_func, headers)
|
|
27
|
+
|
|
28
|
+
def match(self, method: str, path: str,
|
|
29
|
+
request_headers: Optional[Dict[str, str]] = None):
|
|
30
|
+
matched_stub = None
|
|
31
|
+
path_params = {}
|
|
32
|
+
|
|
33
|
+
for compiled_path, (status_code, response, response_func, headers)\
|
|
34
|
+
in self.stubs.get(method, {}).items():
|
|
35
|
+
match = compiled_path.match(path)
|
|
36
|
+
if match:
|
|
37
|
+
if headers and request_headers:
|
|
38
|
+
headers_included = all(
|
|
39
|
+
header_name in request_headers
|
|
40
|
+
and request_headers[header_name] == header_value
|
|
41
|
+
for header_name, header_value in headers
|
|
42
|
+
)
|
|
43
|
+
if not headers_included:
|
|
44
|
+
continue
|
|
45
|
+
|
|
46
|
+
matched_stub = (status_code, response, response_func, headers)
|
|
47
|
+
path_params = match.groupdict()
|
|
48
|
+
break
|
|
49
|
+
|
|
50
|
+
return matched_stub, path_params
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "mimicker"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A lightweight HTTP mocking server for Python"
|
|
5
|
+
authors = ["Amazia Gur <amaziagur@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
keywords = ["http", "mocking", "testing", "mock-server", "stubbing", "ci-cd", "http-server", "testing-tools", "stub-server", "purepython"]
|
|
9
|
+
repository = "https://github.com/amaziahub/mimicker"
|
|
10
|
+
homepage = "https://github.com/amaziahub/mimicker"
|
|
11
|
+
|
|
12
|
+
[tool.poetry.dependencies]
|
|
13
|
+
python = "^3.7"
|
|
14
|
+
requests = "^2.0"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
[tool.poetry.dev-dependencies]
|
|
18
|
+
pytest = "^6.0"
|
|
19
|
+
PyHamcrest = "^2.0"
|
|
20
|
+
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["poetry-core>=1.0.0"]
|
|
23
|
+
build-backend = "poetry.core.masonry.api"
|