Mimicker는 Wiremock에서 영감을 얻은 Python-Native HTTP Mocking Server로, 테스트 목적으로 HTTP 엔드 포인트를 스터 빙 및 조롱하는 프로세스를 단순화하도록 설계되었습니다. Mimicker는 타사 라이브러리가 필요하지 않으며 경량이므로 통합 테스트, 로컬 개발 및 CI 환경에 이상적입니다.
Mimicker는 PIP 또는시를 사용하여 PYPI에서 직접 설치할 수 있습니다.
pip install mimickerpoetry add mimicker간단한 엔드 포인트가있는 특정 포트에서 Mimicker를 시작하려면 다음 코드 스 니펫을 사용할 수 있습니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/hello" ).
body ({ "message" : "Hello, World!" }).
status ( 200 )
)Mimicker는 경로 매개 변수를 동적으로 처리 할 수 있습니다. 경로에서 변수로 엔드 포인트를 조롱하는 방법은 다음과 같습니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/hello/{name}" )
. body ({ "message" : "Hello, {name}!" })
. status ( 200 )
)
# When the client sends a request to /hello/world, the response will be:
# {"message": "Hello, world!"} 사용자 정의 헤더로 응답을 조롱 할 수도 있습니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/hello" )
. body ( "Hello with headers" )
. headers ([( "Content-Type" , "text/plain" ), ( "Custom-Header" , "Value" )])
. status ( 200 )
)
# The response will include custom headers Mimicker를 사용하면 다양한 HTTP 방법 및 경로에 대한 여러 경로를 정의 할 수 있습니다. GET 및 POST Routes의 예는 다음과 같습니다.
from mimicker . mimicker import mimicker , get , post
mimicker ( 8080 ). routes (
get ( "/greet" )
. body ({ "message" : "Hello, world!" })
. status ( 200 ),
post ( "/submit" )
. body ({ "result" : "Submission received" })
. status ( 201 )
)
# Now the server responds to:
# GET /greet -> {"message": "Hello, world!"}
# POST /submit -> {"result": "Submission received"} 동일한 엔드 포인트에 대해 다른 HTTP 상태 코드를 조롱 할 수도 있습니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/status" )
. body ({ "message" : "Success" })
. status ( 200 ),
get ( "/error" )
. body ({ "message" : "Not Found" })
. status ( 404 )
)
# GET /status -> {"message": "Success"} with status 200
# GET /error -> {"message": "Not Found"} with status 404 Mimicker는 JSON 바디를 지원하므로 API 테스트에 이상적입니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/json" )
. body ({ "message" : "Hello, JSON!" })
. status ( 200 )
)
# The response will be: {"message": "Hello, JSON!"} JSON 바디 외에도 Mimicker는 응답 본문의 다른 유형의 컨텐츠를 지원합니다. 다음은 텍스트 또는 파일 콘텐츠를 반환하는 방법입니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/text" )
. body ( "This is a plain text response" )
. status ( 200 )
)
# The response will be plain text: "This is a plain text response" 모의 엔드 포인트에서 파일을 반환 할 수도 있습니다.
from mimicker . mimicker import mimicker , get
mimicker ( 8080 ). routes (
get ( "/file" )
. body ( open ( "example.txt" , "rb" ). read ()) # Mock a file response
. status ( 200 )
)
# The response will be the content of the "example.txt" file get(path) : GET 엔드 포인트를 정의합니다.post(path) : POST 끝점을 정의합니다.put(path) : PUT 엔드 포인트를 정의합니다.delete(path) : DELETE 엔드 포인트를 정의합니다..body(content) : 응답 body 정의합니다..status(code) : 응답 status code 정의합니다..headers(headers) : 응답 headers 정의합니다. Mimicker는 Python 3.7 이상을 지원합니다.
Mimicker는 MIT 라이센스에 따라 릴리스됩니다. 자세한 내용은 라이센스를 참조하십시오.