Skip to content

Commit 5f111e2

Browse files
Add http client (#219)
* Add mini-http-client implementation with json and bytes support * Use anyhow for error handling in the http-client implementation * Remove http-client ClientBuilder * Use local HTTPS server instead of requesting external sources * Move http-client tests as unit tests * refactor http-client tests * fix: update deprecated openssl functions * fixup: tests and cfg fmts also clippy fixes * move http tests to test_async * test_http_client, fix wasm * ci: make certs first require certs to startup * ci: fix wasm test name * fix: ci windows * ci: macos, fixup cert labels * ci: macos, fixup openssl1 vs http-server key fmt expectations * result.rs use anyhow result * ci: add binstall wsocat doesn't release direct binaries so binstall is not used there * ci: dynamically select test port for http-client --------- Co-authored-by: Matheus Consoli <[email protected]>
1 parent 030c3ff commit 5f111e2

File tree

15 files changed

+411
-21
lines changed

15 files changed

+411
-21
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ jobs:
2323
- uses: Swatinem/rust-cache@v2
2424
with:
2525
key: windows-latest-test
26+
- uses: cargo-bins/cargo-binstall@main
27+
- name: Test Setup
28+
run: |
29+
make certs
30+
cargo binstall http-server
2631
- name: Test
27-
run: cargo test --features task,subscriber,fixture,task_unstable,io,sync,future,net,tls,rust_tls,timer,fs,zero_copy,mmap,retry
32+
run: |
33+
Start-Process cmd -Args /c,"http-server --tls --tls-key certs/test-certs/server.key --tls-cert certs/test-certs/server.crt --tls-key-algorithm pkcs8"
34+
cargo test --features task,subscriber,fixture,task_unstable,io,sync,future,net,tls,rust_tls,timer,fs,zero_copy,mmap,retry,http-client-json,__skip-http-client-cert-verification
2835
test:
2936
name: Check ${{ matrix.check }} on (${{ matrix.os }})
3037
runs-on: ${{ matrix.os }}
@@ -54,6 +61,7 @@ jobs:
5461
- uses: Swatinem/rust-cache@v2
5562
with:
5663
key: ${{ matrix.os }}-${{ matrix.check }}
64+
- uses: cargo-bins/cargo-binstall@main
5765
- name: Clippy
5866
if: ${{ matrix.check == 'clippy' }}
5967
run: make check-clippy
@@ -68,7 +76,7 @@ jobs:
6876
if: ${{ matrix.check == 'test' && matrix.os == 'macos-latest'}}
6977
timeout-minutes: 15
7078
# macos-latest is by default on openssl 1.x
71-
run: make PFX_OPTS="" test-all
79+
run: make PFX_OPTS="" CERT_OPTS=cert-patch-macos test-all
7280
- name: cargo audit
7381
if: ${{ matrix.check == 'audit' }}
7482
timeout-minutes: 15
@@ -77,7 +85,7 @@ jobs:
7785
token: ${{ secrets.GITHUB_TOKEN }}
7886

7987
wasm_test:
80-
name: ${{ matrix.test.name }} on (${{ matrix.os }})
88+
name: Wasm test on (${{ matrix.os }})
8189
runs-on: ${{ matrix.os }}
8290
strategy:
8391
# fail-fast: false
@@ -98,6 +106,7 @@ jobs:
98106
toolchain: ${{ matrix.rust }}
99107
profile: minimal
100108
target: wasm32-unknown-unknown
109+
- uses: cargo-bins/cargo-binstall@main
101110
- name: Run Wasm Test on Linux
102111
if: matrix.test == 'linux-wasm'
103112
run: |

Cargo.lock

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

Cargo.toml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ http-client = [
5555
"dep:tokio",
5656
"dep:hyper",
5757
"dep:anyhow",
58+
"dep:webpki-roots",
59+
"dep:http",
60+
"dep:serde",
61+
"dep:serde_json",
62+
"dep:once_cell",
5863
]
5964
tokio1 = ["async-std/tokio1"]
65+
http-client-json = ["http-client"]
66+
# useful for tests
67+
__skip-http-client-cert-verification = []
6068

6169
[dependencies]
6270
log = "0.4.0"
@@ -73,6 +81,11 @@ cfg-if = { version = "1.0.0", optional = true }
7381
tokio = { version = "1.33.0", default-features = false, optional = true }
7482
hyper = { version = "0.14.27", default-features = false, features = ["client", "http1", "http2"], optional = true }
7583
anyhow = { version = "1.0.75", optional = true }
84+
http = { version = "0.2.9", optional = true }
85+
webpki-roots = { version = "0.25.2", optional = true }
86+
serde = { version = "1.0.189", optional = true }
87+
serde_json = { version = "1.0.107", optional = true }
88+
once_cell = { version = "1.18.0", optional = true }
7689
thiserror = "1.0.20"
7790

7891
fluvio-test-derive = { path = "async-test-derive", version = "0.1.1", optional = true }
@@ -98,6 +111,7 @@ async-std = { version = "1.12.0", default-features = false, features = ["unstabl
98111
ws_stream_wasm = "0.7.3"
99112

100113
[dev-dependencies]
114+
async-std = { version = "1.12.0", features = ["attributes"] }
101115
bytes = "1.0.0"
102116
lazy_static = "1.2.0"
103117
num_cpus = "1.10.1"
@@ -107,7 +121,8 @@ tokio-util = { version = "0.7.0", features = ["codec", "compat"] }
107121
tokio = { version = "1.17.0", features = ["macros"] }
108122
flv-util = { version = "0.5.0", features = ["fixture"] }
109123
fluvio-test-derive = { path = "async-test-derive", version = "0.1.0" }
110-
fluvio-future = { path = ".", features = ["net", "fixture", "timer", "fs", "retry"] }
124+
fluvio-future = { path = ".", features = ["net", "fixture", "timer", "fs", "retry", "http-client-json", "__skip-http-client-cert-verification"] }
125+
serde = { version = "1.0.189", features = ["derive"] }
111126

112127
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
113128
portpicker = "0.1.1"

Makefile

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RUST_DOCKER_IMAGE=rust:latest
2-
PFX_OPTS ?= "-legacy"
2+
CERT_OPTS ?=
3+
PFX_OPTS ?= ""
34

45
build-all:
56
cargo build --all-features
@@ -8,8 +9,20 @@ build-all:
89
certs:
910
make -C certs generate-certs PFX_OPTS=${PFX_OPTS}
1011

11-
test-all: certs test-derive
12-
cargo test --all-features
12+
cert-patch-macos:
13+
sed -i '' 's/RSA PRIVATE KEY/PRIVATE KEY/' certs/test-certs/server-hs.key
14+
15+
.PHONY: test-all run-test-all
16+
test-all: certs test-derive setup-http-server run-test-all
17+
run-test-all:
18+
TEST_PORT=$$(cat tmp-PORT) cargo test --all-features
19+
$(MAKE) teardown-http-server
20+
21+
.PHONY: test-http run-test-http
22+
test-http: certs setup-http-server run-test-http
23+
run-test-http:
24+
TEST_PORT=$$(cat tmp-PORT) cargo test --all-features test_http_client
25+
$(MAKE) teardown-http-server
1326

1427
install-wasm-pack:
1528
which wasm-pack || curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
@@ -54,6 +67,20 @@ install-clippy:
5467
install-wasm32:
5568
rustup target add wasm32-unknown-unknown
5669

70+
setup-http-server: certs $(CERT_OPTS)
71+
cargo binstall http-server
72+
cargo binstall -y portpicker-cli
73+
portpicker > tmp-PORT
74+
echo Picked port $$(cat tmp-PORT)
75+
http-server --tls \
76+
--tls-key certs/test-certs/server-hs.key \
77+
--tls-cert certs/test-certs/server.crt \
78+
--tls-key-algorithm pkcs8 -p $$(cat tmp-PORT) &
79+
80+
teardown-http-server:
81+
killall http-server
82+
rm -f tmp-PORT
83+
5784
check-clippy: install-clippy install-wasm32
5885
cargo clippy --all-features
5986
cargo check --target wasm32-unknown-unknown --all-features

certs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ generate-intermediate-ca-crt: generate-intermediate-ca-csr
5050

5151
generate-server-key:
5252
openssl genrsa -out test-certs/server.key 4096
53-
53+
cp test-certs/server.key test-certs/server-hs.key
5454

5555
generate-server-csr: generate-server-key
5656
openssl req -new -key test-certs/server.key \

src/fs/mmap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod tests {
149149
#[test_async]
150150
async fn test_mmap_write_slice() -> Result<(), IoError> {
151151
let index_path = temp_dir().join("test.index");
152-
ensure_clean_file(&index_path.clone());
152+
ensure_clean_file(index_path.clone());
153153

154154
let result = MemoryMappedMutFile::create(&index_path, 3).await;
155155
assert!(result.is_ok());
@@ -176,7 +176,7 @@ mod tests {
176176
#[test_async]
177177
async fn test_mmap_write_pair_slice() -> Result<(), IoError> {
178178
let index_path = temp_dir().join("pairslice.index");
179-
ensure_clean_file(&index_path.clone());
179+
ensure_clean_file(index_path.clone());
180180

181181
let result = MemoryMappedMutFile::create(&index_path, 24).await;
182182
assert!(result.is_ok());
@@ -205,7 +205,7 @@ mod tests {
205205
#[test_async]
206206
async fn test_mmap_write_with_pos() -> Result<(), IoError> {
207207
let index_path = temp_dir().join("testpos.index");
208-
ensure_clean_file(&index_path.clone());
208+
ensure_clean_file(index_path.clone());
209209

210210
let (mut mm_file, _) = MemoryMappedMutFile::create(&index_path, 10).await?;
211211

@@ -227,7 +227,7 @@ mod tests {
227227
#[test_async]
228228
async fn test_empty_index_read_only() -> Result<(), IoError> {
229229
let index_path = temp_dir().join("zerosized.index");
230-
ensure_clean_file(&index_path.clone());
230+
ensure_clean_file(index_path.clone());
231231
{
232232
let file = OpenOptions::new()
233233
.create(true)

0 commit comments

Comments
 (0)