pact-proxy-rs

Crates.iopact-proxy-rs
lib.rspact-proxy-rs
version0.0.1
sourcesrc
created_at2023-01-16 04:28:34.912885
updated_at2023-01-16 04:28:34.912885
descriptionProxy server that records and serves result from PACT files
homepage
repositoryhttps://github.com/NikZak/pact-proxy-rs
max_upload_size
id759838
size126,109
Nikolay Zakirov (NikZak)

documentation

README

Run proxy server that listens to http requests, records requests and responses in PACT format and sends back the response to the sender.

Useful in unit tests. First time test fetches the data from the web and runs slow, but after that it runs fast because it uses the recorded data.

The request sent to it has to be a GET request in the following format:

http://localhost:<port>/<scheme>/<host>/<path>?<query>

e.g:

http://localhost:8080/http/data.binance.com/api/v3/klines?symbol=ZECUSDT&interval=1w&limit=1

Usage

  1. For Rust app. As a rust wrapper for pact mock server

Add to cargo build

[dependencies]
pact-proxy-rs = { git = "https://github.com/NikZak/pact-proxy-rs.git"}

and the in your test

    use pact_proxy_rs::server::PactServer;
    
    let mut pact_server = PactServer::with_http_server(&PathBuf::from("tests/pacts"), None, None).unwrap();
    let port = pact_server.port().unwrap();
    pact_server.start_non_blocking().await.unwrap();
    ...
    let client = MyClient::new_with_url("http://localhost:".to_owned() + port.as_str() + "/https/yoururl.com"));
    let data = client.get_data();
    ...
    pact_server.stop();

if you don't issue pact_serfer.stop() instruction in the end then test won't finish as it would still have a server waiting for requests.

  1. For non-Rust app. As a standalone server.
cargo run -- [OPTIONS] --pact_files_folder <FILE> -p <PORT>

or

cargo build
target/debug/pact-proxy-rs [OPTIONS] --pact_files_folder <FILE> -p <PORT>

Options:

  -f, --pact_files_folder <FILE>  The folder where pacts files will be recorded
  -p, --port <PORT>               (Optional) The port to run the mock service on, if not set then random port is chose

and the in your test

    ...
    let client = MyClient::new_with_url("http://localhost:".to_owned() + <PORT>.as_str() + "/https/yoururl.com"));
    let data = client.get_data();
    ...

In this mode this is run as a standalone server on localhost on a given port. The server captures requests sent to it and transfers them to target-url. Every received response is recorded to pact file together with request.

Contribution

Looking for contributors. Contributions are welcome.

Done:

  • Transform Http GET request and response to PACT format
  • Record PACT files
  • Serve PACT files
  • Match PACT interaction
  • Transform from PACT format to Http response
  • Run as a standalone server

Not done:

  • Transform Http POST request and response to PACT format (requires changing interaction key/descr to a hash)
  • Accept websocket requests
  • Documentation
  • More tests coverage

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this repository by you, as defined in the Apache-2.0 license, shall be licensed as Apache-2.0 as above, without any additional terms or conditions.

Commit count: 2

cargo fmt