1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::borrow::Cow;

#[derive(Debug, Clone)]
pub struct RequestURI<'a> {
    base: Cow<'a, str>,
    query: Cow<'a, str>,
}

impl<'a> RequestURI<'a> {
    /// Create RequestURI type resource.
    pub fn new<T>(base: T, query: T) -> RequestURI<'a>
        where T: Into<Cow<'a, str>>
    {
        RequestURI {
            base: base.into(),
            query: query.into(),
        }
    }

    /// Construct requesting URL from RequestURI type resource.
    ///
    /// # Examples
    ///
    /// ```
    /// use ruroonga_client;
    /// use ruroonga_client::builtin::command_query::CommandQuery;
    /// let uri_base = ruroonga_client::URIBase::new().build();
    /// let mut command = CommandQuery::new("select");
    /// command.set_argument(vec![("table", "Sites")]);
    /// let url = ruroonga_client::RequestURI::new(uri_base, command.encode()).url();
    /// ```
    pub fn url(self) -> String {
        let url = format!("{}{}", self.base.into_owned(), self.query.into_owned());
        url
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use builtin::command_query::CommandQuery;
    use uri_base::URIBase;

    #[test]
    fn construct_request_uri() {
        let url = RequestURI::new("http://localhost:10041", "/d/status").url();
        assert_eq!("http://localhost:10041/d/status", url)
    }

    #[test]
    fn construct_request_uri_with_owned_str() {
        let url = RequestURI::new("http://localhost:10041".to_string(),
                                  "/d/status".to_string())
            .url();
        assert_eq!("http://localhost:10041/d/status", url)
    }

    #[test]
    fn construct_with_actual_usage() {
        let uri_base = URIBase::new().build();
        let mut command = CommandQuery::new("select");
        command.set_argument(vec![("table", "Sites")]);
        let url = RequestURI::new(uri_base, command.encode()).url();
        assert_eq!("http://localhost:10041/d/select?table=Sites", url)
    }
}