Skip to content

Commit

Permalink
Cleaned up the GraphQL endpoint logic and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed May 2, 2023
1 parent 1568bf9 commit f79c1df
Showing 1 changed file with 100 additions and 47 deletions.
147 changes: 100 additions & 47 deletions lib/registry/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use url::Url;

pub static GLOBAL_CONFIG_DATABASE_FILE_NAME: &str = "wasmer.sqlite";

Expand Down Expand Up @@ -65,22 +66,37 @@ pub struct Registry {
}

pub fn format_graphql(registry: &str) -> String {
let mut registry = registry.to_string();
if registry.contains("wapm.dev") {
registry = "https://registry.wapm.dev/graphql".to_string();
} else if registry.contains("wapm.io") {
registry = "https://registry.wapm.io/graphql".to_string();
if let Ok(mut url) = Url::parse(registry) {
// Looks like we've got a valid URL. Let's try to use it as-is.
if url.has_host() {
if url.path() == "/" {
// make sure we convert http://registry.wapm.io/ to
// http://registry.wapm.io/graphql
url.set_path("/graphql");
}

return url.to_string();
}
}
if !registry.starts_with("https://") {
registry = format!("https://{registry}");

if !registry.contains("://") && !registry.contains('/') {
return endpoint_from_domain_name(registry);
}
if registry.ends_with("/graphql") {
registry
} else if registry.ends_with('/') {
format!("{}graphql", registry)
} else {
format!("{}/graphql", registry)

// looks like we've received something we can't deal with. Just pass it
// through as-is and hopefully it'll either work or the end user can figure
// it out
registry.to_string()
}

/// By convention, something like `"wapm.io"` should be converted to
/// `"https://registry.wapm.io/graphql"`.
fn endpoint_from_domain_name(domain_name: &str) -> String {
if domain_name.contains("localhost") {
return format!("http://{domain_name}/graphql");
}

format!("https://registry.{domain_name}/graphql")
}

fn test_if_registry_present(registry: &str) -> Result<(), String> {
Expand All @@ -95,40 +111,6 @@ pub enum UpdateRegistry {
LeaveAsIs,
}

#[test]
fn test_registries_switch_token() {
let mut registries = MultiRegistry::default();

registries.set_current_registry("https://registry.wapm.dev");
assert_eq!(
registries.get_current_registry(),
"https://registry.wapm.dev/graphql".to_string()
);
registries.set_login_token_for_registry(
"https://registry.wapm.io",
"token1",
UpdateRegistry::LeaveAsIs,
);
assert_eq!(
registries.get_current_registry(),
"https://registry.wapm.dev/graphql".to_string()
);
assert_eq!(
registries.get_login_token_for_registry(&registries.get_current_registry()),
None
);
registries.set_current_registry("https://registry.wapm.io");
assert_eq!(
registries.get_login_token_for_registry(&registries.get_current_registry()),
Some("token1".to_string())
);
registries.clear_current_registry_token();
assert_eq!(
registries.get_login_token_for_registry(&registries.get_current_registry()),
None
);
}

impl MultiRegistry {
/// Gets the current (active) registry URL
pub fn clear_current_registry_token(&mut self) {
Expand Down Expand Up @@ -264,3 +246,74 @@ impl WasmerConfig {
wasmer_dir.join(GLOBAL_CONFIG_DATABASE_FILE_NAME)
}
}

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

#[test]
fn test_registries_switch_token() {
let mut registries = MultiRegistry::default();

registries.set_current_registry("https://registry.wapm.dev");
assert_eq!(
registries.get_current_registry(),
"https://registry.wapm.dev/graphql".to_string()
);
registries.set_login_token_for_registry(
"https://registry.wapm.io",
"token1",
UpdateRegistry::LeaveAsIs,
);
assert_eq!(
registries.get_current_registry(),
"https://registry.wapm.dev/graphql".to_string()
);
assert_eq!(
registries.get_login_token_for_registry(&registries.get_current_registry()),
None
);
registries.set_current_registry("https://registry.wapm.io");
assert_eq!(
registries.get_login_token_for_registry(&registries.get_current_registry()),
Some("token1".to_string())
);
registries.clear_current_registry_token();
assert_eq!(
registries.get_login_token_for_registry(&registries.get_current_registry()),
None
);
}

#[test]
fn format_registry_urls() {
let inputs = [
// Domain names work
("wapm.io", "https://registry.wapm.io/graphql"),
("wapm.dev", "https://registry.wapm.dev/graphql"),
// Plain URLs
(
"https://registry.wapm.dev/graphql",
"https://registry.wapm.dev/graphql",
),
(
"https://registry.wapm.dev/something/else",
"https://registry.wapm.dev/something/else",
),
// We don't automatically prepend the domain name with
// "registry", but we will make sure "/" gets turned into "/graphql"
("https://wapm.dev/", "https://wapm.dev/graphql"),
// local development
(
"http://localhost:8000/graphql",
"http://localhost:8000/graphql",
),
("localhost:8000", "http://localhost:8000/graphql"),
];

for (input, expected) in inputs {
let url = format_graphql(input);
assert_eq!(url, expected);
}
}
}

0 comments on commit f79c1df

Please sign in to comment.