diff options
author | Vincent Ambo <tazjin@gmail.com> | 2018-06-16T15·57+0200 |
---|---|---|
committer | Vincent Ambo <tazjin@gmail.com> | 2018-06-16T15·57+0200 |
commit | 10f23a9dfb28f8f4ca838bad9ee402b0611ef6da (patch) | |
tree | 848dd6939ad35642550ba56a2f0828630974b2e7 /src/tests.rs | |
parent | 71f0afe4b54f449978e60732f492f19694902609 (diff) |
feat(main): Implement parsing of JSON payloads
Stackdriver supports structured JSON payloads in addition to simple plain-text payloads. This commit introduces a new feature in which journaldriver will attempt to parse incoming log messages into JSON vaues and forward them as structured payloads if they are JSON objects. Messages that can not be parsed into JSON objects will continue to be forwarded as plain text messages.
Diffstat (limited to 'src/tests.rs')
-rw-r--r-- | src/tests.rs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 000000000000..623cb6b59981 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,80 @@ +use super::*; +use serde_json::to_string; + +#[test] +fn test_text_entry_serialization() { + let entry = LogEntry { + labels: Value::Null, + payload: Payload::TextPayload { + text_payload: "test entry".into(), + } + }; + + let expected = "{\"labels\":null,\"textPayload\":\"test entry\"}"; + let result = to_string(&entry).expect("serialization failed"); + + assert_eq!(expected, result, "Plain text payload should serialize correctly") +} + +#[test] +fn test_json_entry_serialization() { + let entry = LogEntry { + labels: Value::Null, + payload: Payload::TextPayload { + text_payload: "test entry".into(), + } + }; + + let expected = "{\"labels\":null,\"textPayload\":\"test entry\"}"; + let result = to_string(&entry).expect("serialization failed"); + + assert_eq!(expected, result, "Plain text payload should serialize correctly") +} + +#[test] +fn test_plain_text_payload() { + let message = "plain text payload".into(); + let payload = message_to_payload(Some(message)); + let expected = Payload::TextPayload { + text_payload: "plain text payload".into(), + }; + + assert_eq!(expected, payload, "Plain text payload should be detected correctly"); +} + +#[test] +fn test_empty_payload() { + let payload = message_to_payload(None); + let expected = Payload::TextPayload { + text_payload: "empty log entry".into(), + }; + + assert_eq!(expected, payload, "Empty payload should be handled correctly"); +} + +#[test] +fn test_json_payload() { + let message = "{\"someKey\":\"someValue\", \"otherKey\": 42}".into(); + let payload = message_to_payload(Some(message)); + let expected = Payload::JsonPayload { + json_payload: json!({ + "someKey": "someValue", + "otherKey": 42 + }) + }; + + assert_eq!(expected, payload, "JSON payload should be detected correctly"); +} + +#[test] +fn test_json_no_object() { + // This message can be parsed as valid JSON, but it is not an + // object - it should be returned as a plain-text payload. + let message = "42".into(); + let payload = message_to_payload(Some(message)); + let expected = Payload::TextPayload { + text_payload: "42".into(), + }; + + assert_eq!(expected, payload, "Non-object JSON payload should be plain text"); +} |