tests/test_json.c

changeset 1123
2b83302d595a
parent 1119
ff4d7e76f85a
--- a/tests/test_json.c	Sat Jan 11 12:56:54 2025 +0100
+++ b/tests/test_json.c	Sun Jan 12 13:04:32 2025 +0100
@@ -944,7 +944,7 @@
      * According to RFC-8259 we have to test the following characters:
      *    "    quotation mark
      *    \    reverse solidus
-     *    /    solidus
+     *    /    solidus     ---> we make this optional, see test_json_write_solidus
      *    b    backspace
      *    f    form feed
      *    n    line feed
@@ -954,14 +954,14 @@
      * Also, all unicode characters are encoded that way - in our example the 'ö'.
      */
     CxJsonValue* str = cxJsonCreateString(NULL,
-        "hello\twörld\r\nthis/is\\a \"string\"\b in \a string\f");
+        "hello\twörld\r\nthis is\\a \"string\"\b in \a string\f");
     CxJsonWriter writer = cxJsonWriterCompact();
     CxBuffer buf;
     cxBufferInit(&buf, NULL, 128, NULL, 0);
     CX_TEST_DO {
         cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer);
         CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size),
-            CX_STR("\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis\\/is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\"")));
+            CX_STR("\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\"")));
     }
     cxBufferDestroy(&buf);
     cxJsonValueFree(str);
@@ -970,19 +970,39 @@
 CX_TEST(test_json_write_name_escape) {
     CxJsonValue* obj = cxJsonCreateObj(NULL);
     cxJsonObjPutLiteral(obj,
-        CX_STR("hello\twörld\r\nthis/is\\a \"string\"\b in \a string\f"), CX_JSON_TRUE);
+        CX_STR("hello\twörld\r\nthis is\\a \"string\"\b in \a string\f"), CX_JSON_TRUE);
     CxJsonWriter writer = cxJsonWriterCompact();
     CxBuffer buf;
     cxBufferInit(&buf, NULL, 128, NULL, 0);
     CX_TEST_DO {
         cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer);
         CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size),
-            CX_STR("{\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis\\/is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}")));
+            CX_STR("{\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}")));
     }
     cxBufferDestroy(&buf);
     cxJsonValueFree(obj);
 }
 
+CX_TEST(test_json_write_solidus) {
+    CxJsonValue* str = cxJsonCreateString(NULL,"test/solidus");
+    CxJsonWriter writer = cxJsonWriterCompact();
+    CxBuffer buf;
+    cxBufferInit(&buf, NULL, 16, NULL, 0);
+    CX_TEST_DO {
+        // default: do not escape
+        cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer);
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("\"test/solidus\"")));
+
+        // enable escaping
+        writer.escape_slash = true;
+        cxBufferReset(&buf);
+        cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer);
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("\"test\\/solidus\"")));
+    }
+    cxBufferDestroy(&buf);
+    cxJsonValueFree(str);
+}
+
 CxTestSuite *cx_test_suite_json(void) {
     CxTestSuite *suite = cx_test_suite_new("json");
 
@@ -1008,6 +1028,7 @@
     cx_test_register(suite, test_json_write_frac_max_digits);
     cx_test_register(suite, test_json_write_string_escape);
     cx_test_register(suite, test_json_write_name_escape);
+    cx_test_register(suite, test_json_write_solidus);
     
     return suite;
 }

mercurial