openstack_sdk/api/identity/v3/policy/
head.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! HEAD operation on /v3/policies
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use crate::api::rest_endpoint_prelude::*;
24
25#[derive(Builder, Debug, Clone)]
26#[builder(setter(strip_option))]
27pub struct Request {
28    #[builder(setter(name = "_headers"), default, private)]
29    _headers: Option<HeaderMap>,
30}
31impl Request {
32    /// Create a builder for the endpoint.
33    pub fn builder() -> RequestBuilder {
34        RequestBuilder::default()
35    }
36}
37
38impl RequestBuilder {
39    /// Add a single header to the Policy.
40    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
41where {
42        self._headers
43            .get_or_insert(None)
44            .get_or_insert_with(HeaderMap::new)
45            .insert(header_name, HeaderValue::from_static(header_value));
46        self
47    }
48
49    /// Add multiple headers.
50    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
51    where
52        I: Iterator<Item = T>,
53        T: Into<(Option<HeaderName>, HeaderValue)>,
54    {
55        self._headers
56            .get_or_insert(None)
57            .get_or_insert_with(HeaderMap::new)
58            .extend(iter.map(Into::into));
59        self
60    }
61}
62
63impl RestEndpoint for Request {
64    fn method(&self) -> http::Method {
65        http::Method::HEAD
66    }
67
68    fn endpoint(&self) -> Cow<'static, str> {
69        "policies".to_string().into()
70    }
71
72    fn parameters(&self) -> QueryParams {
73        QueryParams::default()
74    }
75
76    fn service_type(&self) -> ServiceType {
77        ServiceType::Identity
78    }
79
80    fn response_key(&self) -> Option<Cow<'static, str>> {
81        None
82    }
83
84    /// Returns headers to be set into the request
85    fn request_headers(&self) -> Option<&HeaderMap> {
86        self._headers.as_ref()
87    }
88
89    /// Returns required API version
90    fn api_version(&self) -> Option<ApiVersion> {
91        Some(ApiVersion::new(3, 0))
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    #[cfg(feature = "sync")]
99    use crate::api::RawQuery;
100    use crate::test::client::FakeOpenStackClient;
101    use crate::types::ServiceType;
102    use http::{HeaderName, HeaderValue};
103    use httpmock::MockServer;
104
105    #[test]
106    fn test_service_type() {
107        assert_eq!(
108            Request::builder().build().unwrap().service_type(),
109            ServiceType::Identity
110        );
111    }
112
113    #[test]
114    fn test_response_key() {
115        assert!(Request::builder().build().unwrap().response_key().is_none())
116    }
117
118    #[cfg(feature = "sync")]
119    #[test]
120    fn endpoint() {
121        let server = MockServer::start();
122        let client = FakeOpenStackClient::new(server.base_url());
123        let mock = server.mock(|when, then| {
124            when.method(httpmock::Method::HEAD)
125                .path("/policies".to_string());
126
127            then.status(200).header("content-type", "application/json");
128        });
129
130        let endpoint = Request::builder().build().unwrap();
131        let _ = endpoint.raw_query(&client).unwrap();
132        mock.assert();
133    }
134
135    #[cfg(feature = "sync")]
136    #[test]
137    fn endpoint_headers() {
138        let server = MockServer::start();
139        let client = FakeOpenStackClient::new(server.base_url());
140        let mock = server.mock(|when, then| {
141            when.method(httpmock::Method::HEAD)
142                .path("/policies".to_string())
143                .header("foo", "bar")
144                .header("not_foo", "not_bar");
145            then.status(200).header("content-type", "application/json");
146        });
147
148        let endpoint = Request::builder()
149            .headers(
150                [(
151                    Some(HeaderName::from_static("foo")),
152                    HeaderValue::from_static("bar"),
153                )]
154                .into_iter(),
155            )
156            .header("not_foo", "not_bar")
157            .build()
158            .unwrap();
159        let _ = endpoint.raw_query(&client).unwrap();
160        mock.assert();
161    }
162}