07/02/2025

Wire_mock ile API Mocking ve dinamik ayarlama

Wiremock gelistirdigimiz yazilimin harici API servisleri ile olan testlerini yapiyor. Yani örnegin bir yere iste gönderip cevabini alacaksiniz bunu API ile yapiyorsunuz ancak test ve gelistirme asamasinda 1000 lerce istek gönderip karsi taraftan bloklanmak veya oraya bunun icin ücret ödemektense wiremock ile istekleri kaydedip veya manuel olusturup daha sonra karsi tarafa hic istek göndermeden calisabilirsiniz..
Wiremock u sistemimizde docker olarak calistiriyorum.

docker run --rm --name wiremock -e WIREMOCK_OPTIONS='--verbose' -p 9000:8080 wiremock/wiremock:3.11.0

Tabii https de enable edilebilir ancak o durumda kendisi selfsigned bir sertifika olusturuyor simdilik onunla ugrasmak istemiyorum.

2025-02-07 13:56:41.937 Verbose logging enabled
2025-02-07 13:56:42.198 Verbose logging enabled
2025-02-07 13:56:42.246 Using HTTP server impl: Jetty11HttpServer

██     ██ ██ ██████  ███████ ███    ███  ██████   ██████ ██   ██
██     ██ ██ ██   ██ ██      ████  ████ ██    ██ ██      ██  ██
██  █  ██ ██ ██████  █████   ██ ████ ██ ██    ██ ██      █████
██ ███ ██ ██ ██   ██ ██      ██  ██  ██ ██    ██ ██      ██  ██
 ███ ███  ██ ██   ██ ███████ ██      ██  ██████   ██████ ██   ██

----------------------------------------------------------------
|               Cloud: https://wiremock.io/cloud               |
|                                                              |
|               Slack: https://slack.wiremock.org              |
----------------------------------------------------------------

version:                      3.11.0
port:                         8080
enable-browser-proxying:      false
disable-banner:               false
no-request-journal:           false
verbose:                      true

extensions:                   response-template,webhook
2025-02-07 13:56:42.451 Admin request received:
[0:0:0:0:0:0:0:1] - GET /health

Host: [localhost:8080]
User-Agent: [curl/8.5.0]
Accept: [*/*]


2025-02-07 13:57:12.610 Admin request received:
[0:0:0:0:0:0:0:1] - GET /health

Host: [localhost:8080]
User-Agent: [curl/8.5.0]
Accept: [*/*]


2025-02-07 13:57:42.672 Admin request received:
[0:0:0:0:0:0:0:1] - GET /health

Daha sonra istedigimiz API yi olusturuyoruz.

curl -X POST localhost:9000/__admin/mappings   -H 'Content-Type: application/json'   -d '{
    "request": {
      "method": "GET",
      "url": "/test"
    },
    "response": {
      "status": 200,
      "body": "{\"message\": \"Hello, World bre world!\"}",
      "headers": {
        "Content-Type": "application/json"
      }
    }
}'

Daha sonra browser dan http://localhost:9000/test adresinden cevabi görebiliyoruz.