2019-01-30 20:53:57 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <gtest/gtest.h>
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-01-30 20:53:57 +00:00
|
|
|
extern "C" {
|
2019-02-09 02:08:02 +00:00
|
|
|
#include "wsfs/jsonrpc/response.h"
|
2019-01-30 20:53:57 +00:00
|
|
|
}
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
static void wsfs_response_parse_str(
|
|
|
|
char const * buffer,
|
2019-02-09 02:08:02 +00:00
|
|
|
struct wsfs_jsonrpc_response * response)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
|
|
|
size_t length = strlen(buffer);
|
2019-02-09 02:08:02 +00:00
|
|
|
wsfs_jsonrpc_response_init(response, buffer, length);
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:53:57 +00:00
|
|
|
TEST(response_parser, test)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-02-09 02:08:02 +00:00
|
|
|
struct wsfs_jsonrpc_response response;
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// invalid json
|
|
|
|
wsfs_response_parse_str("", &response);
|
|
|
|
ASSERT_NE(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(-1, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// invalid json
|
|
|
|
wsfs_response_parse_str("invalid_json", &response);
|
|
|
|
ASSERT_NE(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(-1, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// no object
|
|
|
|
wsfs_response_parse_str("[]", &response);
|
|
|
|
ASSERT_NE(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(-1, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// empty
|
|
|
|
wsfs_response_parse_str("{}", &response);
|
|
|
|
ASSERT_NE(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(-1, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// no data
|
|
|
|
wsfs_response_parse_str("{\"id\":42}", &response);
|
|
|
|
ASSERT_NE(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(42, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// custom error code
|
|
|
|
wsfs_response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
|
|
|
ASSERT_NE(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(42, response.status);
|
|
|
|
ASSERT_EQ(42, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
// valid response
|
|
|
|
wsfs_response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
|
|
|
ASSERT_EQ(WSFS_GOOD, response.status);
|
|
|
|
ASSERT_EQ(42, response.id);
|
2019-01-30 20:53:57 +00:00
|
|
|
ASSERT_NE(nullptr, response.result);
|
2019-01-27 02:45:03 +00:00
|
|
|
json_decref(response.result);
|
|
|
|
}
|