Skip to content

Commit 98e6454

Browse files
committed
feat: improved vetis customization
1 parent 93442b1 commit 98e6454

7 files changed

Lines changed: 93 additions & 64 deletions

File tree

Cargo.lock

Lines changed: 24 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "easyhttpmock"
3-
version = "0.1.3-beta.2"
3+
version = "0.1.3-beta.6"
44
edition = "2021"
55
authors = ["Rogerio Araújo <rogerio.araujo@gmail.com>"]
66
repository = "https://github.com/ararog/easyhttpmock"
@@ -34,4 +34,4 @@ hyper-util = "0.1.9"
3434
rand = "0.9.2"
3535
thiserror = "2.0.17"
3636
tokio = { version = "1.0", features = ["full"] }
37-
vetis = { version = "0.1.4-beta.3", optional = true, default-features = false }
37+
vetis = { version = "0.1.4-beta.4", optional = true, default-features = false }

src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub enum EasyHttpMockError {
88

99
#[derive(Debug, Clone, Error, PartialEq)]
1010
pub enum ServerError {
11+
#[error("Server config error: {0}")]
12+
Config(String),
1113
#[error("Server start error: {0}")]
1214
Start(String),
1315
#[error("Server stop error: {0}")]

src/server/adapters/vetis_adapter.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::future::Future;
22

33
use vetis::{
4-
config::{ListenerConfig, SecurityConfig, ServerConfig, VirtualHostConfig},
4+
config::{ListenerConfig, Protocol, SecurityConfig, ServerConfig, VirtualHostConfig},
55
errors::VetisError,
66
server::{
77
path::HandlerPath,
@@ -21,6 +21,7 @@ use crate::{
2121
pub struct VetisAdapterConfigBuilder {
2222
hostname: Option<String>,
2323
interface: String,
24+
protocol: Protocol,
2425
port: u16,
2526
cert: Option<Vec<u8>>,
2627
key: Option<Vec<u8>>,
@@ -52,6 +53,18 @@ impl VetisAdapterConfigBuilder {
5253
self
5354
}
5455

56+
/// Sets the protocol for the server.
57+
///
58+
/// # Arguments
59+
/// * `protocol` - The protocol to set.
60+
///
61+
/// # Returns
62+
/// A new `VetisAdapterConfigBuilder` instance with the protocol set.
63+
pub fn protocol(mut self, protocol: Protocol) -> Self {
64+
self.protocol = protocol;
65+
self
66+
}
67+
5568
/// Sets the port for the server.
5669
///
5770
/// # Arguments
@@ -108,6 +121,7 @@ impl VetisAdapterConfigBuilder {
108121
VetisAdapterConfig {
109122
hostname: self.hostname,
110123
interface: self.interface,
124+
protocol: self.protocol,
111125
port: self.port,
112126
cert: self.cert,
113127
key: self.key,
@@ -121,6 +135,7 @@ impl VetisAdapterConfigBuilder {
121135
pub struct VetisAdapterConfig {
122136
hostname: Option<String>,
123137
interface: String,
138+
protocol: Protocol,
124139
port: u16,
125140
cert: Option<Vec<u8>>,
126141
key: Option<Vec<u8>>,
@@ -141,6 +156,7 @@ impl Default for VetisAdapterConfig {
141156
Self {
142157
hostname: None,
143158
interface: "0.0.0.0".into(),
159+
protocol: Protocol::Http1,
144160
port: 80,
145161
cert: None,
146162
key: None,
@@ -163,6 +179,7 @@ impl VetisAdapterConfig {
163179
VetisAdapterConfigBuilder {
164180
hostname: None,
165181
interface: "0.0.0.0".into(),
182+
protocol: Protocol::Http1,
166183
port: 80,
167184
cert: None,
168185
key: None,
@@ -223,11 +240,14 @@ impl From<VetisAdapterConfig> for ServerConfig {
223240
fn from(config: VetisAdapterConfig) -> Self {
224241
let listener_config = ListenerConfig::builder()
225242
.interface(&config.interface)
243+
.protocol(config.protocol)
226244
.port(config.port)
227-
.build();
245+
.build()
246+
.expect("Failed to build listener config");
228247
ServerConfig::builder()
229248
.add_listener(listener_config)
230249
.build()
250+
.expect("Failed to build server config")
231251
}
232252
}
233253

@@ -353,6 +373,7 @@ impl ServerAdapter for VetisAdapter {
353373

354374
let host_config = VirtualHostConfig::builder()
355375
.hostname(&hostname)
376+
.root_directory("src/tests")
356377
.port(self.config.port());
357378

358379
let host_config = if let Some(((cert, key), ca)) = self
@@ -374,7 +395,8 @@ impl ServerAdapter for VetisAdapter {
374395
.cert_from_bytes(cert.clone())
375396
.key_from_bytes(key.clone())
376397
.ca_cert_from_bytes(ca.clone())
377-
.build(),
398+
.build()
399+
.map_err(|e| EasyHttpMockError::Server(ServerError::Config(e.to_string())))?,
378400
)
379401
} else {
380402
host_config

src/tests/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod easy_http_mock_config_tests {
33
use crate::{
44
config::EasyHttpMockConfig,
55
server::adapters::vetis_adapter::{VetisAdapter, VetisAdapterConfig},
6+
tests::default_protocol,
67
};
78

89
#[test]
@@ -97,6 +98,7 @@ mod easy_http_mock_config_tests {
9798
fn test_easy_http_mock_config_builder_with_server_config() {
9899
let server_config = VetisAdapterConfig::builder()
99100
.interface("127.0.0.1")
101+
.protocol(default_protocol())
100102
.port(3000)
101103
.build();
102104

@@ -126,6 +128,7 @@ mod easy_http_mock_config_tests {
126128
let base_url = "https://test.mock".to_string();
127129
let server_config = VetisAdapterConfig::builder()
128130
.interface("0.0.0.0")
131+
.protocol(default_protocol())
129132
.port(8443)
130133
.build();
131134

@@ -153,6 +156,7 @@ mod easy_http_mock_config_tests {
153156
fn test_easy_http_mock_config_builder_chaining() {
154157
let server_config = VetisAdapterConfig::builder()
155158
.interface("192.168.1.100")
159+
.protocol(default_protocol())
156160
.port(9090)
157161
.build();
158162

@@ -207,12 +211,14 @@ mod integration_tests {
207211
use crate::{
208212
config::EasyHttpMockConfig,
209213
server::adapters::vetis_adapter::{VetisAdapter, VetisAdapterConfig},
214+
tests::default_protocol,
210215
};
211216

212217
#[test]
213218
fn test_config_with_vetis_server_adapter_integration() {
214219
let server_config = VetisAdapterConfig::builder()
215220
.interface("0.0.0.0")
221+
.protocol(default_protocol())
216222
.port(443)
217223
.build();
218224

@@ -236,6 +242,7 @@ mod integration_tests {
236242
.server_config(
237243
VetisAdapterConfig::builder()
238244
.interface("0.0.0.0")
245+
.protocol(default_protocol())
239246
.port(8080)
240247
.build(),
241248
)
@@ -246,6 +253,7 @@ mod integration_tests {
246253
.server_config(
247254
VetisAdapterConfig::builder()
248255
.interface("0.0.0.0")
256+
.protocol(default_protocol())
249257
.port(9090)
250258
.build(),
251259
)
@@ -268,6 +276,7 @@ mod integration_tests {
268276
.base_url(Some("https://immutable.mock".to_string()))
269277
.server_config(
270278
VetisAdapterConfig::builder()
279+
.protocol(default_protocol())
271280
.port(3000)
272281
.build(),
273282
)
@@ -285,6 +294,7 @@ mod integration_tests {
285294
.base_url(Some("https://new.mock".to_string()))
286295
.server_config(
287296
VetisAdapterConfig::builder()
297+
.protocol(default_protocol())
288298
.port(3000)
289299
.build(),
290300
)
@@ -303,11 +313,13 @@ mod integration_tests {
303313
fn test_config_with_different_server_configs() {
304314
let http_config = VetisAdapterConfig::builder()
305315
.interface("0.0.0.0")
316+
.protocol(default_protocol())
306317
.port(80)
307318
.build();
308319

309320
let https_config = VetisAdapterConfig::builder()
310321
.interface("0.0.0.0")
322+
.protocol(default_protocol())
311323
.port(443)
312324
.build();
313325

src/tests/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1+
use vetis::config::Protocol;
2+
13
mod config;
24
mod server;
5+
6+
pub(crate) const fn default_protocol() -> Protocol {
7+
#[cfg(feature = "http1")]
8+
{
9+
Protocol::Http1
10+
}
11+
#[cfg(feature = "http2")]
12+
{
13+
Protocol::Http2
14+
}
15+
#[cfg(feature = "http3")]
16+
{
17+
Protocol::Http3
18+
}
19+
}

0 commit comments

Comments
 (0)