홈페이지>프로그래밍 관련>기타 소스코드

Mimicker는 Wiremock에서 영감을 얻은 Python-Native HTTP Mocking Server로, 테스트 목적으로 HTTP 엔드 포인트를 스터 빙 및 조롱하는 프로세스를 단순화하도록 설계되었습니다. Mimicker는 타사 라이브러리가 필요하지 않으며 경량이므로 통합 테스트, 로컬 개발 및 CI 환경에 이상적입니다.

특징

설치

Mimicker는 PIP 또는시를 사용하여 PYPI에서 직접 설치할 수 있습니다.

PIP 사용 :

pip install mimicker

시 사용 :

poetry 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 방법 및 경로에 대한 여러 경로를 정의 할 수 있습니다. GETPOST 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 

JSON 바디에 대한 조롱 반응

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 

사용 가능한 기능 :

요구 사항

Mimicker는 Python 3.7 이상을 지원합니다.

특허

Mimicker는 MIT 라이센스에 따라 릴리스됩니다. 자세한 내용은 라이센스를 참조하십시오.

확장하다
추가 정보