Utils

Like we did for unit tests, we'll add some utility function to avoid creating HTTP clients for every test. We will use predefined methods to health-check and to send POST and GET requests to microservices included in the application. Create a utils.rs file and import the necessary types:

use cookie::{Cookie, CookieJar};
use rand::{Rng, thread_rng};
use rand::distributions::Alphanumeric;
pub use reqwest::{self, Client, Method, RedirectPolicy, StatusCode};
use reqwest::header::{COOKIE, SET_COOKIE};
use serde::Deserialize;
use std::collections::HashMap;
use std::iter;
use std::time::Duration;
use std::thread;

We will use a Client instance from the reqwest crate, just as we did for the unit test, but we'll need to import extra types: Method to set different HTTP methods exactly; RedirectPolicy to control redirects, since the router microservice will redirect us to other pages; and Client, which will perform those redirects, but we want to turn off this behavior. StatusCode is used to check returned HTTP status codes.

We imported the COOKIE and SET_COOKIE headers to set the values of those headers for requests, and get their values from the responses. But the values of those headers are formal and we need to parse them. To simplify this, we will use the Cookie and CookieJar types of the cookie crate, since the reqwest crate doesn't support cookies now.

Also, we use the rand crate and the imported Alphanumeric distribution from it to generate unique logins for testing, because we will interact with a working application and simply can't restart it now.

Our application contains four microservices with the following addresses, all of which are available from the Docker containers of our application:

const USERS: &str = "http://localhost:8001";
const MAILER: &str = "http://localhost:8002";
const CONTENT: &str = "http://localhost:8003";
const ROUTER: &str = "http://localhost:8000";

We declared addresses as constant, so that we have a single place to update them if necessary:

pub fn url(url: &str, path: &str) -> String {
url.to_owned() + path
}

Also, we need a function to generate random strings that consist of alphanumeric characters:

pub fn rand_str() -> String {
let mut rng = thread_rng();
iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.take(7)
.collect()
}

The preceding code uses a random number generator that's initialized for the current thread, and an iterator that generates random values, to take 7 characters and join them into String values.

Since integration tests work with live systems, we need a function to sleep the current thread:

pub fn wait(s: u64) {
thread::sleep(Duration::from_secs(s));
}

This function is a short alias for the thread::sleep call.

But it's not all about utilities—we also need a universal client to send requests to all working microservices.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.107.85