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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2018 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use exonum::blockchain::{SharedNodeState, Transaction};
use exonum::node::{ApiSender, TransactionSend, create_public_api_handler,
                   create_private_api_handler};
use iron::{IronError, Handler, Chain};
use iron::headers::{ContentType, Headers};
use iron::status::StatusClass;
use iron_test::{request, response};
use serde::{Deserialize, Serialize};
use serde_json;

use std::fmt;

use super::TestKit;

/// Kind of public or private REST API of an Exonum node.
///
/// `ApiKind` allows to use `get*` and `post*` methods of [`TestKitApi`] more safely.
///
/// [`TestKitApi`]: struct.TestKitApi.html
#[derive(Debug)]
pub enum ApiKind {
    /// `api/system` endpoints of the built-in Exonum REST API.
    System,
    /// `api/explorer` endpoints of the built-in Exonum REST API.
    Explorer,
    /// Endpoints corresponding to a service with the specified string identifier.
    Service(&'static str),
}

impl ApiKind {
    fn into_prefix(self) -> String {
        match self {
            ApiKind::System => "api/system".to_string(),
            ApiKind::Explorer => "api/explorer".to_string(),
            ApiKind::Service(name) => format!("api/services/{}", name),
        }
    }
}

/// API encapsulation for the testkit. Allows to execute and synchronously retrieve results
/// for REST-ful endpoints of services.
pub struct TestKitApi {
    public_handler: Chain,
    private_handler: Chain,
    api_sender: ApiSender,
}

impl fmt::Debug for TestKitApi {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.debug_struct("TestKitApi").finish()
    }
}

impl TestKitApi {
    /// Creates a new instance of API.
    pub(crate) fn new(testkit: &TestKit) -> Self {
        use std::sync::Arc;

        let blockchain = &testkit.blockchain;
        let api_state = SharedNodeState::new(10_000);

        TestKitApi {
            public_handler: create_public_api_handler(
                blockchain.clone(),
                Arc::clone(&testkit.mempool),
                api_state.clone(),
                &testkit.api_config,
            ),

            private_handler: create_private_api_handler(
                blockchain.clone(),
                api_state,
                testkit.api_sender.clone(),
            ),

            api_sender: testkit.api_sender.clone(),
        }
    }

    /// Returns the mounting point for public APIs. Useful for intricate testing not covered
    /// by `get*` and `post*` functions.
    pub fn public_handler(&self) -> &Chain {
        &self.public_handler
    }

    /// Returns the mounting point for private APIs. Useful for intricate testing not covered
    /// by `get*` and `post*` functions.
    pub fn private_handler(&self) -> &Chain {
        &self.private_handler
    }

    /// Sends a transaction to the node via `ApiSender`.
    pub fn send<T>(&self, transaction: T)
    where
        T: Into<Box<Transaction>>,
    {
        self.api_sender.send(transaction.into()).expect(
            "Cannot send transaction",
        );
    }

    fn get_internal<H, D>(handler: &H, endpoint: &str, expect_error: bool, is_public: bool) -> D
    where
        H: Handler,
        for<'de> D: Deserialize<'de>,
    {
        let status_class = if expect_error {
            StatusClass::ClientError
        } else {
            StatusClass::Success
        };

        let url = format!("http://localhost:3000/{}", endpoint);
        let resp = request::get(&url, Headers::new(), handler);
        let resp = if expect_error {
            // Support either "normal" or erroneous responses.
            // For example, `Api.not_found_response()` returns the response as `Ok(..)`.
            match resp {
                Ok(resp) => resp,
                Err(IronError { response, .. }) => response,
            }
        } else {
            resp.expect("Got unexpected `Err(..)` response")
        };

        if let Some(ref status) = resp.status {
            if status.class() != status_class {
                panic!("Unexpected response status: {:?}", status);
            }
        } else {
            panic!("Response status not set");
        }

        let resp = response::extract_body_to_string(resp);

        let publicity = if is_public { "" } else { " private" };
        trace!("GET{} {}\nResponse:\n{}\n", publicity, endpoint, resp);

        serde_json::from_str(&resp).unwrap()
    }

    /// Gets information from a public endpoint of the node.
    ///
    /// # Panics
    ///
    /// - Panics if an error occurs during request processing (e.g., the requested endpoint is
    ///  unknown), or if the response has a non-20x response status.
    pub fn get<D>(&self, kind: ApiKind, endpoint: &str) -> D
    where
        for<'de> D: Deserialize<'de>,
    {
        TestKitApi::get_internal(
            &self.public_handler,
            &format!("{}/{}", kind.into_prefix(), endpoint),
            false,
            true,
        )
    }

    /// Gets information from a private endpoint of the node.
    ///
    /// # Panics
    ///
    /// - Panics if an error occurs during request processing (e.g., the requested endpoint is
    ///  unknown), or if the response has a non-20x response status.
    pub fn get_private<D>(&self, kind: ApiKind, endpoint: &str) -> D
    where
        for<'de> D: Deserialize<'de>,
    {
        TestKitApi::get_internal(
            &self.private_handler,
            &format!("{}/{}", kind.into_prefix(), endpoint),
            false,
            false,
        )
    }

    /// Gets an error from a public endpoint of the node.
    ///
    /// # Panics
    ///
    /// - Panics if the response has a non-40x response status.
    pub fn get_err<D>(&self, kind: ApiKind, endpoint: &str) -> D
    where
        for<'de> D: Deserialize<'de>,
    {
        TestKitApi::get_internal(
            &self.public_handler,
            &format!("{}/{}", kind.into_prefix(), endpoint),
            true,
            true,
        )
    }

    fn post_internal<H, T, D>(handler: &H, endpoint: &str, data: &T, is_public: bool) -> D
    where
        H: Handler,
        T: Serialize,
        for<'de> D: Deserialize<'de>,
    {
        let url = format!("http://localhost:3000/{}", endpoint);
        let body = serde_json::to_string(&data).expect("Cannot serialize data to JSON");
        let resp = request::post(
            &url,
            {
                let mut headers = Headers::new();
                headers.set(ContentType::json());
                headers
            },
            &body,
            handler,
        ).expect("Cannot send data");

        let resp = response::extract_body_to_string(resp);

        let publicity = if is_public { "" } else { " private" };
        trace!(
            "POST{} {}\nBody: \n{}\nResponse:\n{}\n",
            publicity,
            endpoint,
            body,
            resp
        );

        serde_json::from_str(&resp).expect("Cannot parse result")
    }

    /// Posts a transaction to the service using the public API. The returned value is the result
    /// of synchronous transaction processing, which includes running the API shim
    /// and `Transaction.verify()`. `Transaction.execute()` is not run until the transaction
    /// gets to a block via one of `create_block*()` methods.
    ///
    /// # Panics
    ///
    /// - Panics if an error occurs during request processing (e.g., the requested endpoint is
    ///  unknown).
    pub fn post<T, D>(&self, kind: ApiKind, endpoint: &str, transaction: &T) -> D
    where
        T: Serialize,
        for<'de> D: Deserialize<'de>,
    {
        TestKitApi::post_internal(
            &self.public_handler,
            &format!("{}/{}", kind.into_prefix(), endpoint),
            transaction,
            true,
        )
    }

    /// Posts a transaction to the service using the private API. The returned value is the result
    /// of synchronous transaction processing, which includes running the API shim
    /// and `Transaction.verify()`. `Transaction.execute()` is not run until the transaction
    /// gets to a block via one of `create_block*()` methods.
    ///
    /// # Panics
    ///
    /// - Panics if an error occurs during request processing (e.g., the requested endpoint is
    ///  unknown).
    pub fn post_private<T, D>(&self, kind: ApiKind, endpoint: &str, transaction: &T) -> D
    where
        T: Serialize,
        for<'de> D: Deserialize<'de>,
    {
        TestKitApi::post_internal(
            &self.private_handler,
            &format!("{}/{}", kind.into_prefix(), endpoint),
            transaction,
            false,
        )
    }
}