1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/test-src/test_response_parser.cc

64 lines
1.6 KiB
C++
Raw Normal View History

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-01-27 02:45:03 +00:00
#include "wsfs/response_parser.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,
struct wsfs_response * response)
{
size_t length = strlen(buffer);
wsfs_response_parse(buffer, length, response);
}
2019-01-30 20:53:57 +00:00
TEST(response_parser, test)
2019-01-27 02:45:03 +00:00
{
struct wsfs_response response;
// 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);
}