add doc
This commit is contained in:
@@ -4,12 +4,20 @@ use regex::Regex;
|
|||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
/// A Regex-based URL generator.
|
||||||
pub struct RandomUrlGenerator<Rng: rand::Rng> {
|
pub struct RandomUrlGenerator<Rng: rand::Rng> {
|
||||||
random_engine: Rng,
|
random_engine: Rng,
|
||||||
random_parts: Box<[RegexPart]>,
|
random_parts: Box<[RegexPart]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Rng: rand::Rng> RandomUrlGenerator<Rng> {
|
impl<Rng: rand::Rng> RandomUrlGenerator<Rng> {
|
||||||
|
/// Create a new RandomUrlGenerator.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `random_engine` - The random engine to use.
|
||||||
|
/// * `template` - The template to use.
|
||||||
|
///
|
||||||
pub fn new(random_engine: Rng, template: &str) -> Result<Self, RandomUrlGeneratorBuildError> {
|
pub fn new(random_engine: Rng, template: &str) -> Result<Self, RandomUrlGeneratorBuildError> {
|
||||||
let re = Regex::new(r"\[([^\]]+)\](?:\{(\d+)\})?")?;
|
let re = Regex::new(r"\[([^\]]+)\](?:\{(\d+)\})?")?;
|
||||||
let mut parts: Vec<RegexPart> = Vec::new();
|
let mut parts: Vec<RegexPart> = Vec::new();
|
||||||
@@ -71,6 +79,7 @@ impl<Rng: rand::Rng> RandomUrlGenerator<Rng> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate a random URL (string)
|
||||||
pub fn generate(&mut self) -> CompactString {
|
pub fn generate(&mut self) -> CompactString {
|
||||||
self.random_parts.iter().fold(
|
self.random_parts.iter().fold(
|
||||||
CompactString::with_capacity(self.expected_length()),
|
CompactString::with_capacity(self.expected_length()),
|
||||||
@@ -91,6 +100,7 @@ impl<Rng: rand::Rng> RandomUrlGenerator<Rng> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate a random URL. Will return error if the URL is invalid.
|
||||||
pub fn generate_url(&mut self) -> Result<Url, url::ParseError> {
|
pub fn generate_url(&mut self) -> Result<Url, url::ParseError> {
|
||||||
let url = self.generate();
|
let url = self.generate();
|
||||||
Url::parse(&url)
|
Url::parse(&url)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use bytes::Bytes;
|
|||||||
use reqwest::header::HeaderMap;
|
use reqwest::header::HeaderMap;
|
||||||
use reqwest::{Client, Method};
|
use reqwest::{Client, Method};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use tower::Service;
|
use tower::Service;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
@@ -11,12 +12,13 @@ use url::Url;
|
|||||||
pub struct SimpleHttpSward {
|
pub struct SimpleHttpSward {
|
||||||
client: Client,
|
client: Client,
|
||||||
method: Method,
|
method: Method,
|
||||||
headers: HeaderMap,
|
headers: Arc<HeaderMap>,
|
||||||
sent_count: usize,
|
sent_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimpleHttpSward {
|
impl SimpleHttpSward {
|
||||||
pub fn new(client: Client, method: Method, headers: HeaderMap) -> Self {
|
/// Create a new simple http sward.
|
||||||
|
pub fn new(client: Client, method: Method, headers: Arc<HeaderMap>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
client,
|
client,
|
||||||
method,
|
method,
|
||||||
@@ -24,6 +26,8 @@ impl SimpleHttpSward {
|
|||||||
sent_count: 0,
|
sent_count: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the number of requests sent.
|
||||||
pub fn sent_count(&self) -> usize {
|
pub fn sent_count(&self) -> usize {
|
||||||
self.sent_count
|
self.sent_count
|
||||||
}
|
}
|
||||||
@@ -47,7 +51,7 @@ impl Service<SimpleHttpRequest> for SimpleHttpSward {
|
|||||||
Box::pin(
|
Box::pin(
|
||||||
self.client
|
self.client
|
||||||
.request(self.method.clone(), req.url)
|
.request(self.method.clone(), req.url)
|
||||||
.headers(self.headers.clone())
|
.headers(self.headers.as_ref().clone())
|
||||||
.send(),
|
.send(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user