vibe.inet.webform 18/95(18%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
431
441
450
461
470
480
491
501
511
520
530
540
550
561
570
580
590
600
610
620
630
640
650
660
670
683
690
702
712
720
730
740
750
760
770
780
790
800
810
822
833
840
850
860
870
882
892
900
9153
922
932
944
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4830
4840
4850
4860
4870
4880
4890
4900
4910
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6110
6120
6130
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
/** Contains HTML/urlencoded form parsing and construction routines. Copyright: © 2012-2014 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig, Jan Krüger */ module vibe.inet.webform; import vibe.core.file; import vibe.core.log; import vibe.core.path; import vibe.inet.message; import vibe.stream.operations; import vibe.textfilter.urlencode; import vibe.utils.string; import vibe.utils.dictionarylist; import std.range : isOutputRange; import std.traits : ValueType, KeyType; import std.array; import std.exception; import std.string; /** Parses form data according to an HTTP Content-Type header. Writes the form fields into a key-value of type $(D FormFields), parsed from the specified $(D InputStream) and using the corresponding Content-Type header. Parsing is gracefully aborted if the Content-Type header is unrelated. Params: fields = The key-value map to which form fields must be written files = The $(D FilePart)s mapped to the corresponding key in which details on transmitted files will be written to. content_type = The value of the Content-Type HTTP header. body_reader = A valid $(D InputSteram) data stream consumed by the parser. max_line_length = The byte-sized maximum length of lines used as boundary delimitors in Multi-Part forms. */ bool parseFormData(ref FormFields fields, ref FilePartFormFields files, string content_type, InputStream body_reader, size_t max_line_length) @safe { auto ct_entries = content_type.split(";"); if (!ct_entries.length) return false; switch (ct_entries[0].strip()) { default: return false; case "application/x-www-form-urlencoded": parseURLEncodedForm(body_reader.readAllUTF8(), fields); break; case "multipart/form-data": parseMultiPartForm(fields, files, content_type, body_reader, max_line_length); break; } return false; } /** Parses a URL encoded form and stores the key/value pairs. Writes to the $(D FormFields) the key-value map associated to an "application/x-www-form-urlencoded" MIME formatted string, ie. all '+' characters are considered as ' ' spaces. */ void parseURLEncodedForm(string str, ref FormFields params) @safe { while (str.length > 0) { // name part auto idx = str.indexOf("="); if (idx == -1) { idx = vibe.utils.string.indexOfAny(str, "&;"); if (idx == -1) { params.addField(formDecode(str[0 .. $]), ""); return; } else { params.addField(formDecode(str[0 .. idx]), ""); str = str[idx+1 .. $]; continue; } } else { auto idx_amp = vibe.utils.string.indexOfAny(str, "&;"); if (idx_amp > -1 && idx_amp < idx) { params.addField(formDecode(str[0 .. idx_amp]), ""); str = str[idx_amp+1 .. $]; continue; } else { string name = formDecode(str[0 .. idx]); str = str[idx+1 .. $]; // value part for( idx = 0; idx < str.length && str[idx] != '&' && str[idx] != ';'; idx++) {} string value = formDecode(str[0 .. idx]); params.addField(name, value); str = idx < str.length ? str[idx+1 .. $] : null; } } } } /** This example demonstrates parsing using all known form separators, it builds a key-value map into the destination $(D FormFields) */ unittest { FormFields dst; parseURLEncodedForm("a=b;c;dee=asd&e=fgh&f=j%20l", dst); assert("a" in dst && dst["a"] == "b"); assert("c" in dst && dst["c"] == ""); assert("dee" in dst && dst["dee"] == "asd"); assert("e" in dst && dst["e"] == "fgh"); assert("f" in dst && dst["f"] == "j l"); } /** Parses a form in "multipart/form-data" format. If any files are contained in the form, they are written to temporary files using $(D vibe.core.file.createTempFile) and their details returned in the files field. Params: fields = The key-value map to which form fields must be written files = The $(D FilePart)s mapped to the corresponding key in which details on transmitted files will be written to. content_type = The value of the Content-Type HTTP header. body_reader = A valid $(D InputSteram) data stream consumed by the parser. max_line_length = The byte-sized maximum length of lines used as boundary delimitors in Multi-Part forms. */ void parseMultiPartForm(InputStream)(ref FormFields fields, ref FilePartFormFields files, string content_type, InputStream body_reader, size_t max_line_length) if (isInputStream!InputStream) { import std.algorithm : strip; auto pos = content_type.indexOf("boundary="); enforce(pos >= 0 , "no boundary for multipart form found"); auto boundary = content_type[pos+9 .. $].strip('"'); auto firstBoundary = () @trusted { return cast(string)body_reader.readLine(max_line_length); } (); enforce(firstBoundary == "--" ~ boundary, "Invalid multipart form data!"); while (parseMultipartFormPart(body_reader, fields, files, cast(const(ubyte)[])("\r\n--" ~ boundary), max_line_length)) {} } alias FormFields = DictionaryList!(string, true, 16); alias FilePartFormFields = DictionaryList!(FilePart, true, 0); @safe unittest { import vibe.stream.memory; auto content_type = "multipart/form-data; boundary=\"AaB03x\""; auto input = createMemoryStream(cast(ubyte[])( "--AaB03x\r\n" ~ "Content-Disposition: form-data; name=\"submit-name\"\r\n" ~ "\r\n" ~ "Larry\r\n" ~ "--AaB03x\r\n" ~ "Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\r\n" ~ "Content-Type: text/plain\r\n" ~ "\r\n" ~ "... contents of file1.txt ...\r\n" ~ "--AaB03x--\r\n").dup, false); FormFields fields; FilePartFormFields files; parseMultiPartForm(fields, files, content_type, input, 4096); assert(fields["submit-name"] == "Larry"); assert(files["files"].filename == "file1.txt"); } unittest { // issue #1220 - wrong handling of Content-Length import vibe.stream.memory; auto content_type = "multipart/form-data; boundary=\"AaB03x\""; auto input = createMemoryStream(cast(ubyte[])( "--AaB03x\r\n" ~ "Content-Disposition: form-data; name=\"submit-name\"\r\n" ~ "\r\n" ~ "Larry\r\n" ~ "--AaB03x\r\n" ~ "Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\r\n" ~ "Content-Type: text/plain\r\n" ~ "Content-Length: 29\r\n" ~ "\r\n" ~ "... contents of file1.txt ...\r\n" ~ "--AaB03x--\r\n" ~ "Content-Disposition: form-data; name=\"files\"; filename=\"file2.txt\"\r\n" ~ "Content-Type: text/plain\r\n" ~ "\r\n" ~ "... contents of file1.txt ...\r\n" ~ "--AaB03x--\r\n").dup, false); FormFields fields; FilePartFormFields files; parseMultiPartForm(fields, files, content_type, input, 4096); assert(fields["submit-name"] == "Larry"); assert(files["files"].filename == "file1.txt"); } unittest { // use of unquoted strings in Content-Disposition import vibe.stream.memory; auto content_type = "multipart/form-data; boundary=\"AaB03x\""; auto input = createMemoryStream(cast(ubyte[])( "--AaB03x\r\n" ~ "Content-Disposition: form-data; name=submitname\r\n" ~ "\r\n" ~ "Larry\r\n" ~ "--AaB03x\r\n" ~ "Content-Disposition: form-data; name=files; filename=file1.txt\r\n" ~ "Content-Type: text/plain\r\n" ~ "Content-Length: 29\r\n" ~ "\r\n" ~ "... contents of file1.txt ...\r\n" ~ "--AaB03x--\r\n").dup, false); FormFields fields; FilePartFormFields files; parseMultiPartForm(fields, files, content_type, input, 4096); assert(fields["submitname"] == "Larry"); assert(files["files"].filename == "file1.txt"); } /** Single part of a multipart form. A FilePart is the data structure for individual "multipart/form-data" parts according to RFC 1867 section 7. */ struct FilePart { InetHeaderMap headers; NativePath.Segment filename; NativePath tempPath; // avoids NativePath.Segment.toString() being called string toString() const { return filename.name; } } private bool parseMultipartFormPart(InputStream)(InputStream stream, ref FormFields form, ref FilePartFormFields files, const(ubyte)[] boundary, size_t max_line_length) if (isInputStream!InputStream) { //find end of quoted string auto indexOfQuote(string str) { foreach (i, ch; str) { if (ch == '"' && (i == 0 || str[i-1] != '\\')) return i; } return -1; } auto parseValue(ref string str) { string res; if (str[0]=='"') { str = str[1..$]; auto pos = indexOfQuote(str); res = str[0..pos].replace(`\"`, `"`); str = str[pos..$]; } else { auto pos = str.indexOf(';'); if (pos < 0) { res = str; str = ""; } else { res = str[0 .. pos]; str = str[pos..$]; } } return res; } InetHeaderMap headers; stream.parseRFC5322Header(headers); auto pv = "Content-Disposition" in headers; enforce(pv, "invalid multipart"); auto cd = *pv; string name; auto pos = cd.indexOf("name="); if (pos >= 0) { cd = cd[pos+5 .. $]; name = parseValue(cd); } string filename; pos = cd.indexOf("filename="); if (pos >= 0) { cd = cd[pos+9 .. $]; filename = parseValue(cd); } if (filename.length > 0) { FilePart fp; fp.headers = headers; version (Have_vibe_core) fp.filename = NativePath.Segment(filename); else fp.filename = PathEntry.validateFilename(filename); auto file = createTempFile(); fp.tempPath = file.path; if (auto plen = "Content-Length" in headers) { import std.conv : to; stream.pipe(file, (*plen).to!long); enforce(stream.skipBytes(boundary), "Missing multi-part end boundary marker."); } else stream.readUntil(file, boundary); logDebug("file: %s", fp.tempPath.toString()); file.close(); files.addField(name, fp); // TODO: temp files must be deleted after the request has been processed! } else { auto data = () @trusted { return cast(string)stream.readUntil(boundary); } (); form.addField(name, data); } ubyte[2] ub; stream.read(ub, IOMode.all); if (ub == "--") { stream.pipe(nullSink()); return false; } enforce(ub == cast(const(ubyte)[])"\r\n"); return true; } /** Encodes a Key-Value map into a form URL encoded string. Writes to the $(D OutputRange) an application/x-www-form-urlencoded MIME formatted string, ie. all spaces ' ' are replaced by the '+' character Params: dst = The destination $(D OutputRange) where the resulting string must be written to. map = An iterable key-value map iterable with $(D foreach(string key, string value; map)). sep = A valid form separator, common values are '&' or ';' */ void formEncode(R, T)(auto ref R dst, T map, char sep = '&') if (isFormMap!T && isOutputRange!(R, char)) { formEncodeImpl(dst, map, sep, true); } /** The following example demonstrates the use of $(D formEncode) with a json map, the ordering of keys will be preserved in $(D Bson) and $(D DictionaryList) objects only. */ unittest { import std.array : Appender; string[string] map; map["numbers"] = "123456789"; map["spaces"] = "1 2 3 4 a b c d"; Appender!string app; app.formEncode(map); assert(app.data == "spaces=1+2+3+4+a+b+c+d&numbers=123456789" || app.data == "numbers=123456789&spaces=1+2+3+4+a+b+c+d"); } /** Encodes a Key-Value map into a form URL encoded string. Returns an application/x-www-form-urlencoded MIME formatted string, ie. all spaces ' ' are replaced by the '+' character Params: map = An iterable key-value map iterable with $(D foreach(string key, string value; map)). sep = A valid form separator, common values are '&' or ';' */ string formEncode(T)(T map, char sep = '&') if (isFormMap!T) { return formEncodeImpl(map, sep, true); } /** Writes to the $(D OutputRange) an URL encoded string as specified in RFC 3986 section 2 Params: dst = The destination $(D OutputRange) where the resulting string must be written to. map = An iterable key-value map iterable with $(D foreach(string key, string value; map)). */ void urlEncode(R, T)(auto ref R dst, T map) if (isFormMap!T && isOutputRange!(R, char)) { formEncodeImpl(dst, map, "&", false); } /** Returns an URL encoded string as specified in RFC 3986 section 2 Params: map = An iterable key-value map iterable with $(D foreach(string key, string value; map)). */ string urlEncode(T)(T map) if (isFormMap!T) { return formEncodeImpl(map, '&', false); } /** Tests if a given type is suitable for storing a web form. Types that define iteration support with the key typed as $(D string) and the value either also typed as $(D string), or as a $(D vibe.data.json.Json) like value. The latter case specifically requires a $(D .type) property that is tested for equality with $(D T.Type.string), as well as a $(D .get!string) method. */ template isFormMap(T) { import std.conv; enum isFormMap = isStringMap!T || isJsonLike!T; } private template isStringMap(T) { enum isStringMap = __traits(compiles, () { foreach (string key, string value; T.init) {} } ()); } unittest { static assert(isStringMap!(string[string])); static struct M { int opApply(int delegate(string key, string value)) { return 0; } } static assert(isStringMap!M); } private template isJsonLike(T) { enum isJsonLike = __traits(compiles, () { import std.conv; string r; foreach (string key, value; T.init) r = value.type == T.Type.string ? value.get!string : value.to!string; } ()); } unittest { import vibe.data.json; import vibe.data.bson; static assert(isJsonLike!Json); static assert(isJsonLike!Bson); } private string formEncodeImpl(T)(T map, char sep, bool form_encode) if (isStringMap!T) { import std.array : Appender; Appender!string dst; size_t len; foreach (key, ref value; map) { len += key.length; len += value.length; } // characters will be expanded, better use more space the first time and avoid additional allocations dst.reserve(len*2); dst.formEncodeImpl(map, sep, form_encode); return dst.data; } private string formEncodeImpl(T)(T map, char sep, bool form_encode) if (isJsonLike!T) { import std.array : Appender; Appender!string dst; size_t len; foreach (string key, T value; map) { len += key.length; len += value.length; } // characters will be expanded, better use more space the first time and avoid additional allocations dst.reserve(len*2); dst.formEncodeImpl(map, sep, form_encode); return dst.data; } private void formEncodeImpl(R, T)(auto ref R dst, T map, char sep, bool form_encode) if (isOutputRange!(R, string) && isStringMap!T) { bool flag; foreach (key, value; map) { if (flag) dst.put(sep); else flag = true; filterURLEncode(dst, key, null, form_encode); dst.put("="); filterURLEncode(dst, value, null, form_encode); } } private void formEncodeImpl(R, T)(auto ref R dst, T map, char sep, bool form_encode) if (isOutputRange!(R, string) && isJsonLike!T) { bool flag; foreach (string key, T value; map) { if (flag) dst.put(sep); else flag = true; filterURLEncode(dst, key, null, form_encode); dst.put("="); if (value.type == T.Type.string) filterURLEncode(dst, value.get!string, null, form_encode); else { static if (T.stringof == "Json") filterURLEncode(dst, value.to!string, null, form_encode); else filterURLEncode(dst, value.toString(), null, form_encode); } } } unittest { import vibe.utils.dictionarylist : DictionaryList; import vibe.data.json : Json; import vibe.data.bson : Bson; string[string] aaMap; DictionaryList!string dlMap; Json jsonMap = Json.emptyObject; Bson bsonMap = Bson.emptyObject; aaMap["unicode"] = "╤╳"; aaMap["numbers"] = "123456789"; aaMap["spaces"] = "1 2 3 4 a b c d"; aaMap["slashes"] = "1/2/3/4/5"; aaMap["equals"] = "1=2=3=4=5=6=7"; aaMap["complex"] = "╤╳/=$$\"'1!2()'\""; aaMap["╤╳"] = "1"; dlMap["unicode"] = "╤╳"; dlMap["numbers"] = "123456789"; dlMap["spaces"] = "1 2 3 4 a b c d"; dlMap["slashes"] = "1/2/3/4/5"; dlMap["equals"] = "1=2=3=4=5=6=7"; dlMap["complex"] = "╤╳/=$$\"'1!2()'\""; dlMap["╤╳"] = "1"; jsonMap["unicode"] = "╤╳"; jsonMap["numbers"] = "123456789"; jsonMap["spaces"] = "1 2 3 4 a b c d"; jsonMap["slashes"] = "1/2/3/4/5"; jsonMap["equals"] = "1=2=3=4=5=6=7"; jsonMap["complex"] = "╤╳/=$$\"'1!2()'\""; jsonMap["╤╳"] = "1"; bsonMap["unicode"] = "╤╳"; bsonMap["numbers"] = "123456789"; bsonMap["spaces"] = "1 2 3 4 a b c d"; bsonMap["slashes"] = "1/2/3/4/5"; bsonMap["equals"] = "1=2=3=4=5=6=7"; bsonMap["complex"] = "╤╳/=$$\"'1!2()'\""; bsonMap["╤╳"] = "1"; assert(urlEncode(aaMap) == "complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&unicode=%E2%95%A4%E2%95%B3&spaces=1%202%203%204%20a%20b%20c%20d&numbers=123456789&slashes=1%2F2%2F3%2F4%2F5&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&%E2%95%A4%E2%95%B3=1" || urlEncode(aaMap) == "slashes=1%2F2%2F3%2F4%2F5&spaces=1%202%203%204%20a%20b%20c%20d&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&%E2%95%A4%E2%95%B3=1&unicode=%E2%95%A4%E2%95%B3&numbers=123456789&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22"); assert(urlEncode(dlMap) == "unicode=%E2%95%A4%E2%95%B3&numbers=123456789&spaces=1%202%203%204%20a%20b%20c%20d&slashes=1%2F2%2F3%2F4%2F5&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&%E2%95%A4%E2%95%B3=1"); assert(urlEncode(jsonMap) == "%E2%95%A4%E2%95%B3=1&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&unicode=%E2%95%A4%E2%95%B3&spaces=1%202%203%204%20a%20b%20c%20d&numbers=123456789&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&slashes=1%2F2%2F3%2F4%2F5" || urlEncode(jsonMap) == "slashes=1%2F2%2F3%2F4%2F5&spaces=1%202%203%204%20a%20b%20c%20d&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&%E2%95%A4%E2%95%B3=1&unicode=%E2%95%A4%E2%95%B3&numbers=123456789&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22"); assert(urlEncode(bsonMap) == "unicode=%E2%95%A4%E2%95%B3&numbers=123456789&spaces=1%202%203%204%20a%20b%20c%20d&slashes=1%2F2%2F3%2F4%2F5&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&%E2%95%A4%E2%95%B3=1"); { FormFields aaFields; parseURLEncodedForm(urlEncode(aaMap), aaFields); assert(urlEncode(aaMap) == urlEncode(aaFields)); FormFields dlFields; parseURLEncodedForm(urlEncode(dlMap), dlFields); assert(urlEncode(dlMap) == urlEncode(dlFields)); FormFields jsonFields; parseURLEncodedForm(urlEncode(jsonMap), jsonFields); assert(urlEncode(jsonMap) == urlEncode(jsonFields)); FormFields bsonFields; parseURLEncodedForm(urlEncode(bsonMap), bsonFields); assert(urlEncode(bsonMap) == urlEncode(bsonFields)); } assert(formEncode(aaMap) == "complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&unicode=%E2%95%A4%E2%95%B3&spaces=1+2+3+4+a+b+c+d&numbers=123456789&slashes=1%2F2%2F3%2F4%2F5&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&%E2%95%A4%E2%95%B3=1" || formEncode(aaMap) == "slashes=1%2F2%2F3%2F4%2F5&spaces=1+2+3+4+a+b+c+d&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&%E2%95%A4%E2%95%B3=1&unicode=%E2%95%A4%E2%95%B3&numbers=123456789&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22"); assert(formEncode(dlMap) == "unicode=%E2%95%A4%E2%95%B3&numbers=123456789&spaces=1+2+3+4+a+b+c+d&slashes=1%2F2%2F3%2F4%2F5&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&%E2%95%A4%E2%95%B3=1"); assert(formEncode(jsonMap) == "%E2%95%A4%E2%95%B3=1&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&unicode=%E2%95%A4%E2%95%B3&spaces=1+2+3+4+a+b+c+d&numbers=123456789&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&slashes=1%2F2%2F3%2F4%2F5" || formEncode(jsonMap) == "slashes=1%2F2%2F3%2F4%2F5&spaces=1+2+3+4+a+b+c+d&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&%E2%95%A4%E2%95%B3=1&unicode=%E2%95%A4%E2%95%B3&numbers=123456789&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22"); assert(formEncode(bsonMap) == "unicode=%E2%95%A4%E2%95%B3&numbers=123456789&spaces=1+2+3+4+a+b+c+d&slashes=1%2F2%2F3%2F4%2F5&equals=1%3D2%3D3%3D4%3D5%3D6%3D7&complex=%E2%95%A4%E2%95%B3%2F%3D%24%24%22%271%212%28%29%27%22&%E2%95%A4%E2%95%B3=1"); { FormFields aaFields; parseURLEncodedForm(formEncode(aaMap), aaFields); assert(formEncode(aaMap) == formEncode(aaFields)); FormFields dlFields; parseURLEncodedForm(formEncode(dlMap), dlFields); assert(formEncode(dlMap) == formEncode(dlFields)); FormFields jsonFields; parseURLEncodedForm(formEncode(jsonMap), jsonFields); assert(formEncode(jsonMap) == formEncode(jsonFields)); FormFields bsonFields; parseURLEncodedForm(formEncode(bsonMap), bsonFields); assert(formEncode(bsonMap) == formEncode(bsonFields)); } }