Mimicker เป็นเซิร์ฟเวอร์การเยาะเย้ย HTTP แบบ python-native ที่ได้รับแรงบันดาลใจจาก Wiremock ซึ่งออกแบบมาเพื่อทำให้กระบวนการของจุดสิ้นสุดของ HTTP มีจุดประสงค์เพื่อการทดสอบ Mimicker ไม่จำเป็นต้องมีห้องสมุดบุคคลที่สามและมีน้ำหนักเบาทำให้เหมาะสำหรับการทดสอบการรวมการพัฒนาในท้องถิ่นและสภาพแวดล้อม CI
Mimicker สามารถติดตั้งได้โดยตรงจาก PYPI โดยใช้ PIP หรือบทกวี:
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 :
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) : กำหนดจุด GETpost(path) : กำหนดจุดสิ้นสุด POSTput(path) : กำหนดจุด PUTdelete(path) : กำหนดจุดสิ้นสุด DELETE.body(content) : กำหนด body การตอบสนอง.status(code) : กำหนด status code การตอบกลับ.headers(headers) : กำหนด headers การตอบสนอง Mimicker รองรับ Python 3.7 ขึ้นไป
Mimicker เปิดตัวภายใต้ใบอนุญาต MIT ดูใบอนุญาตสำหรับข้อมูลเพิ่มเติม