ホーム>プログラミング関連>その他のソースコード

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はパスパラメーターを動的に処理できます。パスの変数でエンドポイントをock笑する方法は次のとおりです。

 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ルートの例です。

 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 Bodiesをサポートしており、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ライセンスの下でリリースされます。詳細については、ライセンスを参照してください。

拡大する
追加情報