node/test/cctest/inspector/test_node_protocol.cc
Chengzhong Wu 0edeafd73d
inspector: fix StringUtil::CharacterCount for unicodes
`StringUtil::CharacterCount` should return the length of underlying
representation storage of a protocol string.

`StringUtil::CharacterCount` is only used in DictionaryValue
serialization. Only `Network.Headers` is an object type, represented
with DictionaryValue.

PR-URL: https://github.com/nodejs/node/pull/56788
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
2025-01-29 14:58:25 +00:00

33 lines
970 B
C++

#include "crdtp/json.h"
#include "gtest/gtest.h"
#include "inspector/node_json.h"
#include "node/inspector/protocol/Protocol.h"
namespace node {
namespace inspector {
namespace protocol {
namespace {
TEST(InspectorProtocol, Utf8StringSerDes) {
constexpr const char* kKey = "unicode_key";
constexpr const char* kValue = "CJK 汉字 🍱 🧑‍🧑‍🧒‍🧒";
std::unique_ptr<DictionaryValue> val = DictionaryValue::create();
val->setString(kKey, kValue);
std::vector<uint8_t> cbor = val->Serialize();
std::string json;
crdtp::Status status =
crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
CHECK(status.ok());
std::unique_ptr<DictionaryValue> parsed =
DictionaryValue::cast(JsonUtil::parseJSON(json));
std::string parsed_value;
CHECK(parsed->getString(kKey, &parsed_value));
CHECK_EQ(parsed_value, std::string(kValue));
}
} // namespace
} // namespace protocol
} // namespace inspector
} // namespace node