|
| 1 | +use url::Url; |
| 2 | + |
| 3 | +use crate::{plugins::PluginListParams, ApiBaseUrl, WPContext}; |
| 4 | + |
| 5 | +pub struct PluginsEndpoint { |
| 6 | + api_base_url: ApiBaseUrl, |
| 7 | +} |
| 8 | + |
| 9 | +impl PluginsEndpoint { |
| 10 | + pub fn new(api_base_url: ApiBaseUrl) -> Self { |
| 11 | + Self { api_base_url } |
| 12 | + } |
| 13 | + |
| 14 | + pub fn create(&self) -> Url { |
| 15 | + self.plugins_base_url() |
| 16 | + } |
| 17 | + |
| 18 | + pub fn delete(&self, plugin: &str) -> Url { |
| 19 | + self.plugins_url_with_slug(plugin) |
| 20 | + } |
| 21 | + |
| 22 | + pub fn list(&self, context: WPContext, params: Option<&PluginListParams>) -> Url { |
| 23 | + let mut url = self.plugins_base_url(); |
| 24 | + url.query_pairs_mut() |
| 25 | + .append_pair("context", context.as_str()); |
| 26 | + if let Some(params) = params { |
| 27 | + url.query_pairs_mut().extend_pairs(params.query_pairs()); |
| 28 | + } |
| 29 | + url |
| 30 | + } |
| 31 | + |
| 32 | + pub fn retrieve(&self, context: WPContext, plugin: &str) -> Url { |
| 33 | + let mut url = self.plugins_url_with_slug(plugin); |
| 34 | + url.query_pairs_mut() |
| 35 | + .append_pair("context", context.as_str()); |
| 36 | + url |
| 37 | + } |
| 38 | + |
| 39 | + pub fn update(&self, plugin: &str) -> Url { |
| 40 | + self.plugins_url_with_slug(plugin) |
| 41 | + } |
| 42 | + |
| 43 | + fn plugins_base_url(&self) -> Url { |
| 44 | + self.api_base_url.by_appending("plugins") |
| 45 | + } |
| 46 | + |
| 47 | + fn plugins_url_with_slug(&self, plugin: &str) -> Url { |
| 48 | + self.api_base_url |
| 49 | + // The '/' character has to be preserved and not get encoded |
| 50 | + .by_extending(["plugins"].into_iter().chain(plugin.split('/'))) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use super::*; |
| 57 | + use crate::{ |
| 58 | + endpoint::tests::{fixture_api_base_url, validate_endpoint}, |
| 59 | + generate, ApiEndpoint, PluginStatus, |
| 60 | + }; |
| 61 | + use rstest::*; |
| 62 | + |
| 63 | + #[rstest] |
| 64 | + fn create_plugin(plugins_endpoint: PluginsEndpoint) { |
| 65 | + validate_endpoint(plugins_endpoint.create(), "/plugins"); |
| 66 | + } |
| 67 | + |
| 68 | + #[rstest] |
| 69 | + #[case("hello-dolly/hello", "/plugins/hello-dolly/hello")] |
| 70 | + #[case( |
| 71 | + "classic-editor/classic-editor", |
| 72 | + "/plugins/classic-editor/classic-editor" |
| 73 | + )] |
| 74 | + #[case("foo/bar%baz", "/plugins/foo/bar%25baz")] |
| 75 | + #[case("foo/です", "/plugins/foo/%E3%81%A7%E3%81%99")] |
| 76 | + fn delete_plugin( |
| 77 | + plugins_endpoint: PluginsEndpoint, |
| 78 | + #[case] plugin_slug: &str, |
| 79 | + #[case] expected_path: &str, |
| 80 | + ) { |
| 81 | + validate_endpoint(plugins_endpoint.delete(plugin_slug), expected_path); |
| 82 | + } |
| 83 | + |
| 84 | + #[rstest] |
| 85 | + #[case(WPContext::Edit, generate!(PluginListParams, (search, Some("foo".to_string()))), "/plugins?context=edit&search=foo")] |
| 86 | + #[case(WPContext::Embed, generate!(PluginListParams, (status, Some(PluginStatus::Active))), "/plugins?context=embed&status=active")] |
| 87 | + #[case(WPContext::View, generate!(PluginListParams, (search, Some("foo".to_string())), (status, Some(PluginStatus::Inactive))), "/plugins?context=view&search=foo&status=inactive")] |
| 88 | + fn list_plugins_with_params( |
| 89 | + plugins_endpoint: PluginsEndpoint, |
| 90 | + #[case] context: WPContext, |
| 91 | + #[case] params: PluginListParams, |
| 92 | + #[case] expected_path: &str, |
| 93 | + ) { |
| 94 | + validate_endpoint(plugins_endpoint.list(context, Some(¶ms)), expected_path); |
| 95 | + } |
| 96 | + |
| 97 | + #[rstest] |
| 98 | + #[case( |
| 99 | + "hello-dolly/hello", |
| 100 | + WPContext::View, |
| 101 | + "/plugins/hello-dolly/hello?context=view" |
| 102 | + )] |
| 103 | + #[case( |
| 104 | + "classic-editor/classic-editor", |
| 105 | + WPContext::Embed, |
| 106 | + "/plugins/classic-editor/classic-editor?context=embed" |
| 107 | + )] |
| 108 | + #[case("foo/bar%baz", WPContext::Edit, "/plugins/foo/bar%25baz?context=edit")] |
| 109 | + #[case( |
| 110 | + "foo/です", |
| 111 | + WPContext::View, |
| 112 | + "/plugins/foo/%E3%81%A7%E3%81%99?context=view" |
| 113 | + )] |
| 114 | + fn retrieve_plugin( |
| 115 | + plugins_endpoint: PluginsEndpoint, |
| 116 | + #[case] plugin_slug: &str, |
| 117 | + #[case] context: WPContext, |
| 118 | + #[case] expected_path: &str, |
| 119 | + ) { |
| 120 | + validate_endpoint( |
| 121 | + plugins_endpoint.retrieve(context, plugin_slug), |
| 122 | + expected_path, |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + #[rstest] |
| 127 | + #[case("hello-dolly/hello", "/plugins/hello-dolly/hello")] |
| 128 | + #[case( |
| 129 | + "classic-editor/classic-editor", |
| 130 | + "/plugins/classic-editor/classic-editor" |
| 131 | + )] |
| 132 | + #[case("foo/bar%baz", "/plugins/foo/bar%25baz")] |
| 133 | + #[case("foo/です", "/plugins/foo/%E3%81%A7%E3%81%99")] |
| 134 | + fn update_plugin( |
| 135 | + plugins_endpoint: PluginsEndpoint, |
| 136 | + #[case] plugin_slug: &str, |
| 137 | + #[case] expected_path: &str, |
| 138 | + ) { |
| 139 | + validate_endpoint(plugins_endpoint.update(plugin_slug), expected_path); |
| 140 | + } |
| 141 | + |
| 142 | + #[fixture] |
| 143 | + fn plugins_endpoint(fixture_api_base_url: ApiBaseUrl) -> PluginsEndpoint { |
| 144 | + ApiEndpoint::new(fixture_api_base_url).plugins |
| 145 | + } |
| 146 | +} |
0 commit comments