Advertisement
Guest User

Untitled

a guest
May 4th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. global.TEST_DATABASE = "mongodb://localhost/ca4test";
  2.  
  3. var should = require("should");
  4. var app = require("../../server/app");
  5. var http = require("http");
  6. var testPort = 9999;
  7. var testServer;
  8. var mongoose = require("mongoose");
  9. var db = require('../../server/model/db.js');
  10. var querystring = require('querystring');
  11.  
  12. describe('REST API for /getWiki', function () {
  13. before(function (done) {
  14. testServer = app.listen(testPort, function () {
  15. console.log("Server is listening on: " + testPort);
  16. done();
  17. })
  18. .on('error',function(err){
  19. console.log(err);
  20. });
  21. })
  22.  
  23. beforeEach(function(done){
  24. var array = [{title : "Test wiki",
  25. url :"www.test.dk",
  26. abstract: "Lorem ipsum dolor sit",
  27. categories: ['cat1', 'cat2'],
  28. headings: [{heading: 'Test heading', position: 1}, {heading: 'Test heading 2', position: 2}]
  29. }];
  30. db.WikiModel.create(array,function(err){
  31.  
  32. });
  33. done();
  34. })
  35.  
  36. after(function(){
  37. mongoose.connection.db.dropDatabase();
  38. testServer.close();
  39. })
  40.  
  41. it("Should return json object of wiki based on title", function (done) {
  42.  
  43. var query = querystring.stringify({
  44. 'title' : 'Test wiki'
  45. });
  46.  
  47. var options = {
  48. hostname: 'localhost',
  49. port: testPort,
  50. path: '/api/getWiki'+query,
  51. method: 'GET',
  52. headers: {
  53. 'Content-Type': 'JSON'
  54. }
  55. };
  56.  
  57. http.request(options, function(res) {
  58. res.setEncoding('utf8');
  59. res.on('data', function (chunk) {
  60. var wiki = JSON.parse(chunk);
  61. wiki.should.be.json;
  62. wiki.title.should.equal('Test wiki');
  63. wiki.abstract.should.equal('Lorem ipsum dolor sit');
  64. wiki.categories.length.should.equal(2);
  65. wiki.headings.length.should.equal(2);
  66. wiki.headings[0].should.be.an.instanceOf(Object);
  67. done();
  68. });
  69. });
  70. done();
  71. });
  72.  
  73. it("Should return json object of wiki based on title", function (done) {
  74.  
  75. var query = querystring.stringify({
  76. 'title' : 'Test wiki'
  77. });
  78.  
  79. var options = {
  80. hostname: 'localhost',
  81. port: testPort,
  82. path: '/api/findWiki'+query,
  83. method: 'GET',
  84. headers: {
  85. 'Content-Type': 'JSON'
  86. }
  87. };
  88.  
  89. http.request(options, function(res) {
  90. res.setEncoding('utf8');
  91. res.on('data', function (chunk) {
  92. var wiki = JSON.parse(chunk);
  93. wiki.should.be.json;
  94. wiki.title.should.equal('Test wiki');
  95. wiki.abstract.should.equal('Lorem ipsum dolor sit');
  96. wiki.categories.length.should.equal(2);
  97. wiki.headings.length.should.equal(2);
  98. wiki.headings[0].should.be.an.instanceOf(Object);
  99. done();
  100. });
  101. });
  102. done();
  103. });
  104. it("Should return json array of all wikis", function (done) {
  105.  
  106.  
  107. var options = {
  108. hostname: 'localhost',
  109. port: testPort,
  110. path: '/api/getWikis',
  111. method: 'GET',
  112. headers: {
  113. 'Content-Type': 'JSON'
  114. }
  115. };
  116.  
  117. http.request(options, function(res) {
  118. res.setEncoding('utf8');
  119. res.on('data', function (chunk) {
  120. var wiki = JSON.parse(chunk);
  121. wiki.should.be.json;
  122. wiki.length.should.equal(2);
  123. wiki[0].title.should.equal('Test wiki');
  124. wiki[0].abstract.should.equal('Lorem ipsum dolor sit');
  125. wiki[0].categories.length.should.equal(2);
  126. wiki[0].headings.length.should.equal(2);
  127. wiki[0].headings[0].should.be.an.instanceOf(Object);
  128.  
  129. wiki[1].title.should.equal('Test wiki2');
  130. wiki[1].abstract.should.equal('bla bla bla');
  131. wiki[1].categories.length.should.equal(2);
  132. wiki[1].headings.length.should.equal(2);
  133. wiki[1].headings[0].should.be.an.instanceOf(Object);
  134. done();
  135. });
  136. });
  137. done();
  138. });
  139. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement