หน้าแรก>การเขียนโปรแกรมที่เกี่ยวข้อง>ซอร์สโค้ดอื่น ๆ

Mimicker เป็นเซิร์ฟเวอร์การเยาะเย้ย HTTP แบบ python-native ที่ได้รับแรงบันดาลใจจาก Wiremock ซึ่งออกแบบมาเพื่อทำให้กระบวนการของจุดสิ้นสุดของ HTTP มีจุดประสงค์เพื่อการทดสอบ Mimicker ไม่จำเป็นต้องมีห้องสมุดบุคคลที่สามและมีน้ำหนักเบาทำให้เหมาะสำหรับการทดสอบการรวมการพัฒนาในท้องถิ่นและสภาพแวดล้อม CI

คุณสมบัติ

การติดตั้ง

Mimicker สามารถติดตั้งได้โดยตรงจาก PYPI โดยใช้ PIP หรือบทกวี:

ใช้ 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 ที่แตกต่างกัน นี่คือตัวอย่างของเส้นทาง GET และ POST :

 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 ดูใบอนุญาตสำหรับข้อมูลเพิ่มเติม

ขยาย
ข้อมูลเพิ่มเติม