Skip to content

Commit

Permalink
Add wasm tests using was-bindgen-test and wasm-pack (#346)
Browse files Browse the repository at this point in the history
* Add wasm tests using was-bindgen-test and wasm-pack

* Remove custom wasm feature

* Add #[wasm_bindgen_test] to remaining tests

* Try to fix CI

---------

Co-authored-by: Vincent Prouillet <github@vincentprouillet.com>
  • Loading branch information
lpotthast and Keats committed Dec 1, 2023
1 parent 6f978d0 commit 459cdad
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 49 deletions.
90 changes: 48 additions & 42 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ on:
- master
pull_request:


jobs:
style:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check format
run: cargo fmt --check
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check format
run: cargo fmt --check

clippy:
name: Clippy
Expand All @@ -36,42 +35,49 @@ jobs:
matrix:
build: [pinned, stable, nightly]
include:
- build: pinned
os: ubuntu-20.04
rust: 1.67.0
- build: stable
os: ubuntu-20.04
rust: stable
- build: nightly
os: ubuntu-20.04
rust: nightly
- build: pinned
os: ubuntu-20.04
rust: 1.67.0
- build: stable
os: ubuntu-20.04
rust: stable
- build: nightly
os: ubuntu-20.04
rust: nightly
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}

- name: Build System Info
run: rustc --version

- name: Run tests default features
run: cargo test

- name: Run tests no features
run: cargo test --no-default-features

wasm:
name: Run tests in wasm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown

- uses: actions/setup-node@v4

- name: Build System Info
run: rustc --version
- name: Install wasm-pack
run: cargo install wasm-pack

- name: Run tests default features
run: cargo test
- name: Run tests default features
run: wasm-pack test --node

- name: Run tests no features
run: cargo test --no-default-features
- name: Run tests no features
run: wasm-pack test --node --no-default-features

# wasm:
# name: Run tests in wasm
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - name: Install Rust
# uses: dtolnay/rust-toolchain@stable
# with:
# targets: wasm32-unknown-unknown
# - name: Run tests default features
# run: cargo test --target wasm32-unknown-unknown
#
# - name: Run tests no features
# run: cargo test --no-default-features --target wasm32-unknown-unknown
24 changes: 19 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,44 @@ homepage = "https://github.com/Keats/jsonwebtoken"
repository = "https://github.com/Keats/jsonwebtoken"
keywords = ["jwt", "api", "token", "jwk"]
edition = "2021"
include = ["src/**/*", "benches/**/*", "tests/**/*", "LICENSE", "README.md", "CHANGELOG.md"]
include = [
"src/**/*",
"benches/**/*",
"tests/**/*",
"LICENSE",
"README.md",
"CHANGELOG.md",
]
rust-version = "1.67.0"

[dependencies]
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
base64 = "0.21.0"
# For PEM decoding
pem = {version = "3", optional = true}
simple_asn1 = {version = "0.6", optional = true}
pem = { version = "3", optional = true }
simple_asn1 = { version = "0.6", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.17.4", features = ["std"] }


[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.1"

[target.'cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))'.dev-dependencies]
# For the custom time example
time = "0.3"
criterion = "0.4"

[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
# For the custom time example
time = { version = "0.3", features = ["wasm-bindgen"] }
criterion = { version = "0.4", default-features = false }

[features]
default = ["use_pem"]
use_pem = ["pem", "simple_asn1"]
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header,
struct Claims {
sub: String,
company: String,
exp: usize,
exp: u64,
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Claims {
aud: String,
sub: String,
company: String,
exp: usize,
exp: u64,
}

fn main() {
Expand Down
3 changes: 3 additions & 0 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ impl Algorithm {

#[cfg(test)]
mod tests {
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;

#[test]
#[wasm_bindgen_test]
fn generate_algorithm_enum_from_str() {
assert!(Algorithm::from_str("HS256").is_ok());
assert!(Algorithm::from_str("HS384").is_ok());
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ impl From<ErrorKind> for Error {

#[cfg(test)]
mod tests {
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;

#[test]
#[wasm_bindgen_test]
fn test_error_rendering() {
assert_eq!(
"InvalidAlgorithmName",
Expand Down
2 changes: 2 additions & 0 deletions src/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,10 @@ mod tests {
use crate::serialization::b64_encode;
use crate::Algorithm;
use serde_json::json;
use wasm_bindgen_test::wasm_bindgen_test;

#[test]
#[wasm_bindgen_test]
fn check_hs256() {
let key = b64_encode("abcdefghijklmnopqrstuvwxyz012345");
let jwks_json = json!({
Expand Down

0 comments on commit 459cdad

Please sign in to comment.