-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
1384 lines (1293 loc) · 42 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
/*** Node.js modules ***/
// a core module
const assert = require('assert');
// npm module
//var
const MongoClient = require('mongodb').MongoClient;
/*** Node.js modules end ***/
/*** config ***/
// If X.json is a file, parse X.json to a JavaScript Object.
//try {pak = require('package.json');}catch(err){pak = null;} ? pak : "fail";
//const
var env = () => {
try {
return require('./.env.json');
} catch(err) {
console.warn("config file missing, so as actual connection info too");
return {
"TEST_MONGODB": {
"value": false
}
,"DEBUG_MODE": {
"value": false
}
};
}
}();
const mongoLab_URI = (
// must be on `.env` file or
// in heroku config
// it is possible
// to use the same config var
// in both local and Heroku environments
env.TEST_MONGODB.value ||
process.env.TEST_MONGODB ||
process.argv[3] ||
"mongodb://localhost:27017/data_uri"
);
//*** config end ***//
//*** application modules ***//
const host_Name_Validator = require('./host_Name_Validator.js');
//.get_Short_Link()
//.choose_Options()
//.get_Short_Link_Length()
const link_Gen = require('./short_link_generator.js');//.short_Link_Generator;
//generate_Unique_Short_Link
// started hole app, not just import designated functions
//const db_Helpers = require('./app.js');
//exports.make_Unique_Link
const db_Helpers = require('./db_Helpers.js');
const comparator = require('./comparator.js');
//*** application modules end ***//
//*** helpers ***//
// DONE create unique fields indexes
// DONE query for (check if) field value in the list
// DONE but not exactly extract info about successfully inserted documents from 'insertMany' result
// for
// col.insertMany([{a:1}, {a:2}], function(err, r) {
// r.insertedCount
function make_Links_Documents(
size//:int
)/* => list of obj */ {
"use strict";
var result;
var results = [];
var loop_Counter = 0;
for (;loop_Counter < size;loop_Counter++){
//{"original_url":"original_Link_1","short_url":""}
// ??? WTF ???
// DONE : test short_Link_Generator for ''
results.push(
{
"original_url": "original_Link_" + loop_Counter,
"short_url": link_Gen.get_Short_Link(loop_Counter)
}
);
}
return results;
}
/*** helpers end ***/
/*** tests ***/
var actual_Results;
var expected_Results;
// case 1: valid host names
var hosts_case_1 = [
"mongodb.github.io",
"node-mongodb-native.api",
"en.wikipedia.org",
"nodejs.org",
"github.com",
"www.w3schools.com",
"www.xn----7sbajojiclh2ahkc2br7fc0m.xn--p1ai"
];
expected_Results = [
true,
true,
true,
true,
true,
true,
true,
];
// case 2: invalid host names
var hosts_case_2 = [
"mongodb..github.io",
"-node-mongodb-native.api",
"en.wikipedia.org-",
"nodejsorg",
"?github.com",
"www.-w3schools.com",
"www.xn----7sbajojiclh2ahkc2br7fc0m.xn--p1ai%"
];
var expected_Results_case_2 = [
false,
false,
false,
false,
false,
false,
false,
];
var test_1 = function(description){
"use strict";
// curred
return function(
hosts//:list of str
,expected_Results//:list of bool
)/* => list of bool */ {
"use strict";
console.log(description);
var hosts_Length = hosts.length;
var host_Index = 0;
var host;
var results = [];
var result;
//for (host_Index in hosts) {
// host = hosts[host_Index];
hosts.forEach((host) => {
//validate_Host_Name
result = host_Name_Validator.is_Host_Name_Valid(host);
results.push(result);
console.log("is ", host,"a valid host name:", result);
}
);
//assert.equal({a: {b: 1}}, {a: {b: 1}});
//AssertionError: [ true, true, true, true, true, true, true ] == [ true, true, true, true, true, true, true ]
//actual_Results = results
assert.deepEqual(results, expected_Results);
return results;
};
}("test 1: must check for a valid host name")
//(hosts_case_1, expected_Results)
//(hosts_case_2, expected_Results_case_2)
;
// DONE 1.drop collection
// DONE 2.dummy data generator
// DONE 3.insert generated data
var docs_List = [
{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_1", "short_url" : "yY"}
];
// case 2: some docs -> unique
var bulk_Docs_List = [
{ "insertOne": { "document": {"original_url" : "o_L_0", "short_url" : "a"} } }
,{ "insertOne": { "document": {"original_url" : "o_L_0", "short_url" : "a"} } }
,{ "insertOne": { "document": {"original_url" : "o_L_0", "short_url" : "a"} } }
,{ "insertOne": { "document": {"original_url" : "o_L_1", "short_url" : "yY"} } }
];
// case 2: some docs -> unique
var bulk_Docs_List_2 = [
{ "insertOne": { "document": {"original_url" : "o_L_0", "short_url" : "b"} } }
,{ "insertOne": { "document": {"original_url" : "o_L_2", "short_url" : "a"} } }
,{ "insertOne": { "document": {"original_url" : "o_L_3", "short_url" : "c"} } }
,{ "insertOne": { "document": {"original_url" : "o_L_1", "short_url" : "yY"} } }
];
var test_2 = function(description){
"use strict";
// curred
return function(
mongoLab_URI//:str
,collection_Name//:str
,documents//:list of obj
//,indexes_List//:list of str
)/* => Promise ? */ {
"use strict";
console.log(description);
var result;
var results = [];
var docs_Number = 7;
//var documents = [];
//console.log("mongoLab_URI is:", mongoLab_URI);
//documents = make_Links_Documents(docs_Number);
console.log("documents: %j", documents);
if (Array.isArray(documents)) {
if (documents.length > 0) {
} else {
return results;
}
//"tests"
//var clear = clear_Links(mongoLab_URI, "tests");
//console.log("typeof clear:", (typeof clear));
//console.log("clear instanceof Promise:", (clear instanceof Promise));
//var connection_Promise = MongoClient.connect(mongoLab_URI);
var collection_Promise = Promise.resolve(db_Helpers.get_Collection(
mongoLab_URI//:str
,collection_Name
,null
,MongoClient
)
);
//connection_Promise
// .then((db) => {
collection_Promise
.then((collection) => {
!(env.DEBUG_MODE.value) || console.log("collection: %j", collection);
db_Helpers.add_Docs(
documents,//:list of obj
collection
);
}
).catch((err) => {
!(env.DEBUG_MODE.value) || console.log("collection_Promise.then err:", err.code);
console.log(err.stack);
}
);
}
//return results;
};
//}("test 2: must drop existing collection, create new one & insert list of documents to it in DB");
}("test 2: must insert list of documents to collection in DB")
//(mongoLab_URI, "tests", docs_List)
//(mongoLab_URI, "tests", bulk_Docs_List)
;
var test_2_1 = function(description){
"use strict";
// curred
return function(
MongoClient//: MongoClient obj <- explicit
,mongoLab_URI//:str
,collection_Name//:str
,documents//:list of obj
)/* => Promise ? */ {
"use strict";
console.log(description);
var result;
var results = [];
//console.log("mongoLab_URI is:", mongoLab_URI);
!(env.DEBUG_MODE.value) || console.log("documents: %j", documents);
var db_Promise = //Promise
//.resolve(
db_Helpers
.bulk_Docs_Insert(
MongoClient//: MongoClient obj <- explicit
,mongoLab_URI//: str
,collection_Name//: str
,documents//:list of obj
//)
);
!(env.DEBUG_MODE.value) || console.log(
"db_Promise instanceof Promise:", (db_Promise instanceof Promise));
return db_Promise;
/*.then((db) => {
console.log("closing db");
db.close;
console.log("db instanceof Promise:", (db instanceof Promise));
console.log("typeof db:", (typeof db));
}
).catch((err) => {
console.log("db_Promise.then() err:", err.code);
console.log(err.stack);
}*/
//);
//return results;
};
}("test 2.1: must insert list of documents to collection in DB using bulkWrite()")
//(MongoClient, mongoLab_URI, "tests", bulk_Docs_List);
//(MongoClient, mongoLab_URI, "tests", bulk_Docs_List_2)
;
var test_3 = function(
description//:str
) {
"use strict";
console.log(description);
var result;
var results = [];
var filtered = [];
results = link_Gen.choose_Options.symbols_List;
console.log("results: %j", results);
filtered = results.filter((i) => i == "");
console.log("filtered results: %j", filtered);
result = filtered.length > 0;
return result;
}
//("test 3: choose_Options must be non-empty")
;
var test_4 = function(
description
,link_Size//:int
,links_Total//:int
) {
"use strict";
console.log(description);
var result;
var results = [];
var filtered = [];
var options_List = link_Gen.choose_Options.symbols_List;
var generate = link_Gen.get_Short_Link;
var loop_Counter = 0;
var link = "";
for (;loop_Counter < links_Total;loop_Counter++) {
link = generate(
link_Size,
options_List
);
results
.push(
link
);
if (link == "") {
filtered.push(link);
}
}
//console.log("results: %j", results);
//filtered = results.filter((i) => i == "");
console.log("filtered results: %j", filtered);
result = filtered.length > 0;
var actual_Results = result;//test_4(1, 150000);
var expected_Results = false;
//assert(actual_Results == expected_Results);
// or (same as)
assert.equal(actual_Results, expected_Results);
return result;
}
//("test 4: all links must be non-empty", 1, 150000)//() <- on / off
;
// case 1: all docs -> unique
/*
db.tests.createIndex(
{short_url: 1}
,{
background: true
,unique: true
}
);
db.tests.insert(
[
{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_1", "short_url" : "b"}
,{"original_url" : "o_L_2", "short_url" : "c"}
,{"original_url" : "o_L_3", "short_url" : "vV"}
],
{ ordered: false }
);
*/
var docs_Case_1 = [
{"original_url" : "o_L_1", "short_url" : "a"}
,{"original_url" : "o_L_2", "short_url" : "b"}
,{"original_url" : "o_L_3", "short_url" : "c"}
,{"original_url" : "o_L_4", "short_url" : "vV"}
];
// case 2: some docs -> unique
/*
db.tests.createIndex({"original_url": 1}, {"unique": true});
db.tests.createIndex({"short_url": 1}, {"unique": true});
db.tests.createIndex({"original_url": 1, "short_url": 1}, {"unique": true});
db.tests.insert({"original_url" : "o_L_0", "short_url" : "a"});
db.tests.insert(
[
{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_1", "short_url" : "c"}
,{"original_url" : "o_L_2", "short_url" : "gG"}
],
{ ordered: false }
);
*/
var docs_Case_2 = [
{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_1", "short_url" : "c"}
,{"original_url" : "o_L_2", "short_url" : "gG"}
];
// case 3: one doc -> unique
/*
db.tests.createIndex({"short_url": 1, "unique": true});
db.tests.insert(
[
{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_1", "short_url" : "yY"}
],
{ ordered: false }
);
*/
var docs_Case_3 = [
{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_0", "short_url" : "a"}
,{"original_url" : "o_L_1", "short_url" : "yY"}
];
var test_5 = function(description){
"use strict";
// curred
return function(
mongoLab_URI//:str
,collection_Name//:str
,documents//:list of obj
//,indexes_List//:list of str
,field_Name//:str
,MongoClient//: MongoClient obj <- explicit
)/* => Promise ? */ {
"use strict";
console.log(description);
var result;
var results = [];
console.log("mongoLab_URI is:", mongoLab_URI);
//results = make_Links_Documents(7);
//console.log("results: %j", results);
//"tests"
var clear = db_Helpers.clear_Links(mongoLab_URI, collection_Name, MongoClient);
//console.log("typeof clear:", (typeof clear));
//console.log("clear instanceof Promise:", (clear instanceof Promise));
///return //Promise.resolve(
clear
.then((db) => {
// ? is it same (one / singleton) db object ?
var collection_Promise = ///Promise.resolve(
db_Helpers
.get_Collection(
mongoLab_URI,//:str
collection_Name//:str
,db
,MongoClient
///)
);
//console.log("typeof collection:", (typeof collection));
//console.log("collection instanceof Promise:", (collection instanceof Promise));
//console.log("collection Promise status:", collection);
///return ///Promise.resolve(
collection_Promise
.then((collection) => {
db_Helpers
.create_Unique_Index(
//"tests"//:str
//collection_Name
collection
//,"short_url"
,field_Name//:str
)//;
/**/
.then((/*col*/) => {
// indexes(callback){Promise}
// Retrieve all the indexes on the collection.
collection
.indexes()
.then((indexes) => {
console.log("collection.indexes:%j", indexes);
// Let's close the db
// something pending & prevent from properly close(ing) db
if (collection.s.db) {
console.log("closing collection.s.db");
collection.s.db.close();
}
// TypeError: Cannot read property 'close' of undefined
if (db) {
console.log("closing db");
db.close();
}
}
).catch((err) => {console.log("collection.indexes.then err:", err.stack);}
);
}
)
/**/
}
)
//.then(Promise.resolve())
/*
// ReferenceError: collection is not defined
.then((col) => {
// Retrieve all the indexes on the collection.
collection
// TypeError: Cannot read property 'indexes' of undefined
.indexes()
.then((indexes) => {
console.log("collection.indexes:%j", indexes);
// Let's close the db
// something pending & prevent from properly close(ing) db
if (collection.s.db) {
console.log("closing collection.s.db");
collection.s.db.close();
}
// TypeError: Cannot read property 'close' of undefined
if (db) {
console.log("closing db");
db.close();
}
}
).catch((err) => {console.log("collection.indexes.then err:", err.stack);}
);
}
)
*/
/*
.then((collection) => {
add_Docs(
documents,//:list of obj
//collection_Name//:str
collection
);
// already handled (within)in above function
//.catch((err) => {console.log("add_Docs.then err:", err.stack);});
}
)*/
.catch((err) => {
//console.log("create_Unique_Index.then err:", err.stack);
console.log("collection_Promise.then err:", err.stack);
}
///)
);
}
).catch((err) => {console.log("clear.then err:", err.stack);}
//)
);
//return results;
};
}("test 5: must drop collection, then create unique index in the new collection");
//(mongoLab_URI, "tests", docs_Case_1, "short_url", MongoClient);
var test_6 = function(description){
"use strict";
// curred
return function(
collection_Name//:str
//,documents//:list of obj
)/* => Promise ? */ {
"use strict";
console.log(description);
actual_Results = db_Helpers
.clear_Links(
mongoLab_URI,//:str
collection_Name//:str
);
console.log("typeof actual_Results:", (typeof actual_Results));
// actual_Results: Promise { <pending> }
console.log("actual_Results:", actual_Results);
// databaseName: 'sandbox_mongo-db_v3-0'
return Promise.resolve(
actual_Results
.then((db) => {
console.log("db:", db);
db.close();
return db;
}
)
);
};
}("test 6: must drop collection & return current DB object");
//("tests");
var test_7 = function(description){
"use strict";
// curred
return function(
collection_Name//:str
,db_Promise//:Promise obj
)/* => Promise ? */ {
"use strict";
console.log(description);
var actual_Results;
// case 2: db passed
db_Promise.then((db) => {
actual_Results = db_Helpers.get_Collection(
mongoLab_URI,//:str
collection_Name//:str
,db
);
console.log("typeof actual_Results:", (typeof actual_Results));
// actual_Results: Promise { collection }
console.log("actual_Results:", actual_Results);
// namespace: 'sandbox_mongo-db_v3-0.tests',
// name: 'tests',
// promiseLibrary: [Function: Promise],
/*
Promise
// `resolves` without `then`
.resolve(
actual_Results
//.then((col) => {console.log("col:", col);})
);
*/
db.close();
}).catch((err) => {console.log("db_Promise then:", err.stack);}
);
};
}("test 7: must return collection object from DB, when db passed");
//("tests", db_Helpers.get_DB(mongoLab_URI));
//works but better use getter from above
//("tests", db_Helpers.clear_Links(mongoLab_URI, "tests"));
var test_8 = function(description){
"use strict";
// curred
return function(
//collection_Name//:str
//,mongoLab_URI//:str
collection_Promise//:Promise obj
//,db_Promise//:Promise obj
)/* => Promise ? */ {
"use strict";
console.log(description);
var actual_Results;
// case 1: no db passed, db_URI only
/*
actual_Results = get_Collection(
mongoLab_URI,//:str
collection_Name//:str
//,db
);
actual_Results*/
collection_Promise
.then((collection) => {
console.log("collection:", collection);
//console.log("typeof actual_Results:", (typeof actual_Results));
// namespace: 'sandbox_mongo-db_v3-0.tests',
// name: 'tests',
// promiseLibrary: [Function: Promise],
//console.log("collection.db:%j", collection.db);
// TypeError: Cannot read property 'close' of undefined
//collection.db.close();
//collection.s.db.close();
var db = collection.s.db;
db.close();
}).catch((err) => {console.log("collection_Promise then:", err.stack);}
);
};
}("test 8: must return collection object from DB, when no db passed");
//(db_Helpers.get_Collection(mongoLab_URI, "tests"));
//("tests", mongoLab_URI);
var test_9_2 = function(description){
"use strict";
// curred
return function(
url//:str
,expected_Result//:list of bool
)/* => list of bool */ {
"use strict";
console.log(description);
var results = [];
var result;
var getter = require('http');
//var getter = require('https');
if (url.slice(0, 5) == 'https') {
url = 'http' + url.slice(5);
}
return Promise.resolve(
getter
.get(
url,
(response) => {
console.log("Got response:", response.statusCode);
console.log("headers: ", response.headers);
var contentType = (
response.getHeader ? response.getHeader('content-type') : response.headers['content-type']);
//readable
//response.resume();
// `explicitly` convert to `Strings`
// rather than standard `Buffer` `objects`
response.setEncoding('utf8');
response
.once(
'data',
(data) => {
// row data Buffer
console.log("data:", data);
}
);
//response.end([data][, encoding][, callback])
//response.body ? console.log("data:", data) : console.log("response.body:", response.body);
//console.log("response.body:", response.body);
// An alias of assert.ok()
// Tests if value is truthy.
// It is equivalent to -> assert.equal(!!value, true, message).
assert(response.statusCode < expected_Result);
//assert.equal(response.statusCode, expected_Result);
//assert.deepEqual(results, expected_Results);
//next();
return contentType;
}
)
.on('error', (err) => {console.log("url getter error:", err.stack);}
)
);
};
}("test 9.2: must receive correct / expected 'statusCode' from response to remote server")
// res.type('.html'); // => 'text/html'
// res.type('html'); // => 'text/html'
// res.get('Content-Type'); => "text/plain"
//("https://soundcloud.com/", 400)
// res.type('json'); // => 'application/json'
// res.type('application/json'); // => 'application/json'
//("https://api-url-shortener-microservice.herokuapp.com/lInK", "application/json")
//("https://api-url-shortener-microservice.herokuapp.com/new/http://expressjs.com/en/4x/api.html#res.type"
//, "application/json")
;
var test_9_1 = function(description){
"use strict";
// curred
return function(
url//:str
,expected_Result//:list of bool
)/* => list of bool */ {
"use strict";
console.log(description);
var results = [];
var result;
var getter = require('https');
//var express = require('express');
//var app = express();
//app.on('mount', callback(parent))
//app.on('error', (err) => {console.log("express error:", err.stack);});
return Promise.resolve(
/*
app
// Routes HTTP GET requests (to the running app.listen(port, [hostname], [backlog], [callback]))
// to the specified path
// with the specified callback functions.
.get(
url,
function (err, req, res, next) {
!(err) || console.log("express error:", err.stack);
result = res.get('Content-Type');
console.log("res.get('Content-Type'):", result);
//res.send('GET request to homepage');
assert(result == expected_Result);
assert.equal(result, expected_Result);
//assert.deepEqual(results, expected_Results);
next();
return result;
}
*/
getter
.get(
url,
(response) => {
console.log(`Got response: ${response.statusCode}`);
console.log('headers: ', response.headers);
var contentType = (
response.getHeader ? response.getHeader('content-type') : response.headers['content-type']);
//readable
//response.resume();
// `explicitly` convert to `Strings`
// rather than standard `Buffer` `objects`
response.setEncoding('utf8');
response
.once(
'data',
(data) => {
// row data Buffer
console.log("data:", data);
}
);
//response.end([data][, encoding][, callback])
//response.body ? console.log("data:", data) : console.log("response.body:", response.body);
//console.log("response.body:", response.body);
assert(contentType == expected_Result);
assert.equal(contentType, expected_Result);
//assert.deepEqual(results, expected_Results);
//next();
return contentType;
}
)
.on('error', (err) => {console.log("url getter error:", err.stack);}
)
);
};
}("test 9.1: must receive correct / expected 'content-type' from response to remote server")
// res.type('.html'); // => 'text/html'
// res.type('html'); // => 'text/html'
// res.get('Content-Type'); => "text/plain"
//("https://api-url-shortener-microservice.herokuapp.com/", "text/html")
// res.type('json'); // => 'application/json'
// res.type('application/json'); // => 'application/json'
//("https://api-url-shortener-microservice.herokuapp.com/lInK", "application/json")
//("https://api-url-shortener-microservice.herokuapp.com/new/http://expressjs.com/en/4x/api.html#res.type"
//, "application/json")
;
/* jshint esversion: 6, laxcomma: true */
/**/
var test_10 = function(description){
"use strict";
// curred
return function(
MongoClient//: MongoClient obj <- explicit
,mongoLab_URI//: str
//,connection//: MongoClient.connect obj <- optional
//,db//: MongoClient.connect.then() obj <- optional
//,collection//: db.collection obj <- optional
,collection_Name//: str
,original_Link//: str
,short_Link_Size//: int
//,documents//: list of obj
//,query//: query obj
) {//: => Promise | thenable ((dict | obj) | undefined | error)
"use strict";
console.log(description);
var connection = MongoClient.connect(mongoLab_URI);
//var db = Promise.resolve(connection.then((db) => {return db;}));
var db;// = connection.then((_db) => {return _db;});
//var db = Promise.resolve(connection.then((db) => {return Promise.resolve(db);}));
var collection;// = Promise.resolve(db.then((_db) => {return Promise.resolve(_db.collection(collection_Name));}));
var result = db_Helpers
.find_Short_Link(
MongoClient//: MongoClient obj <- explicit
,mongoLab_URI//: str
,connection//: MongoClient.connect obj <- optional
,db//: MongoClient.connect.then() obj <- optional
,collection//: db.collection obj <- optional
,collection_Name//: str
,original_Link//: str
,short_Link_Size//: int
,env.DEBUG_MODE.value
);
//if (db && collection) {
//} else if (db) {
//} else {
//return result;
//return Promise.resolve(result
return result
.then((f_R) => {
console.log("f_R.document:", f_R.document, "is_New:", f_R.is_New);
console.log("typeof(f_R.db)", typeof(f_R.db));
if (f_R.db) {
console.log("closing db ...");
f_R.db.close();
}
}
)
.catch((err) => {console.log("result.then():", err.stack);}
//)
);
//}
}
}("test 10: must " +
"using MongoClient, mongoLab_URI without passed db & collection objects\n" +
"find matched documents in collection if any &\n" +
"return as Promise | thenable containing as result:\n" +
"-> new short_Link as (from) 1st non-matched document if any\n" +
"with all (both) field values not in db.collection\n" +
"or -> existing short_url\n" +
"or -> undefined\n" +
"also db object to allow to close opened current connection")
//(MongoClient, mongoLab_URI, "tests", "o_L_0", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_1", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_2", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_3", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_4", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_5", 1)
///(MongoClient, mongoLab_URI, "tests", docs_Case_3)
///(MongoClient, mongoLab_URI, "tests", docs_Case_2)
///(MongoClient, mongoLab_URI, "tests", docs_Case_1)
;
var test_10_2 = function(description){
"use strict";
// curred
return function(
MongoClient//: MongoClient obj <- explicit
,mongoLab_URI//: str
//,connection//: MongoClient.connect obj <- optional
//,db//: MongoClient.connect.then() obj <- optional
//,collection//: db.collection obj <- optional
,collection_Name//: str
,original_Link//: str
,short_Link_Size//: int
//,documents//: list of obj
//,query//: query obj
) {//: => Promise | thenable ((dict | obj) | undefined | error)
"use strict";
console.log(description);
var connection = MongoClient.connect(mongoLab_URI);
return connection
.then((db) => {
var collection;// = Promise.resolve(db.then((_db) => {return Promise.resolve(_db.collection(collection_Name));}));
var result = db_Helpers
.find_Short_Link(
MongoClient//: MongoClient obj <- explicit
,mongoLab_URI//: str
,connection//: MongoClient.connect obj <- optional
,db//: MongoClient.connect.then() obj <- optional
,collection//: db.collection obj <- optional
,collection_Name//: str
,original_Link//: str
,short_Link_Size//: int
,env.DEBUG_MODE.value
);
return result
.then((f_R) => {
console.log("f_R.document:", f_R.document, "is_New:", f_R.is_New);
console.log("typeof(f_R.db)", typeof(f_R.db));
if (f_R.db) {
console.log("closing db ...");
f_R.db.close();
}
}
)
.catch((err) => {console.log("result.then():", err.stack);}
);
}
)
.catch((err) => {console.log("connection.then():", err.stack);}
);
}
}("test 10.2: must " +
"using MongoClient, mongoLab_URI, db without passed collection object\n" +
"find matched documents in collection if any &\n" +
"return as Promise | thenable containing as result:\n" +
"-> new short_Link as (from) 1st non-matched document if any\n" +
"with all (both) field values not in db.collection\n" +
"or -> existing short_url\n" +
"or -> undefined\n" +
"also db object to allow to close opened current connection")
//(MongoClient, mongoLab_URI, "tests", "o_L_0", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_1", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_2", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_3", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_4", 1)
//(MongoClient, mongoLab_URI, "tests", "o_L_5", 1)
///(MongoClient, mongoLab_URI, "tests", docs_Case_3)
///(MongoClient, mongoLab_URI, "tests", docs_Case_2)
///(MongoClient, mongoLab_URI, "tests", docs_Case_1)
;