decoder_spec.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var should = require('should'),
  2. needle = require('./../'),
  3. Q = require('q'),
  4. chardet = require('jschardet');
  5. describe('character encoding', function() {
  6. var url;
  7. this.timeout(5000);
  8. describe('test A', function() {
  9. before(function() {
  10. url = 'http://www.nina.jp/server/slackware/webapp/tomcat_charset.html';
  11. })
  12. describe('with decode = false', function() {
  13. it('does not decode', function(done) {
  14. needle.get(url, { decode: false }, function(err, resp) {
  15. resp.body.should.be.a.String;
  16. chardet.detect(resp.body).encoding.should.eql('windows-1252');
  17. resp.body.indexOf('EUCを使う').should.eql(-1);
  18. done();
  19. })
  20. })
  21. })
  22. describe('with decode = true', function() {
  23. it('decodes', function(done) {
  24. needle.get(url, { decode: true }, function(err, resp) {
  25. resp.body.should.be.a.String;
  26. chardet.detect(resp.body).encoding.should.eql('ascii');
  27. resp.body.indexOf('EUCを使う').should.not.eql(-1);
  28. done();
  29. })
  30. })
  31. })
  32. })
  33. describe('test B', function() {
  34. it('encodes to UTF-8', function(done) {
  35. // Our Needle wrapper that requests a chinese website.
  36. var task = Q.nbind(needle.get, needle, 'http://www.chinesetop100.com/');
  37. // Different instantiations of this task
  38. var tasks = [Q.fcall(task, {decode: true}),
  39. Q.fcall(task, {decode: false})];
  40. var results = tasks.map(function(task) {
  41. return task.then(function(obj) {
  42. return obj[0].body;
  43. });
  44. });
  45. // Execute all requests concurrently
  46. Q.all(results).done(function(bodies) {
  47. var charsets = [
  48. chardet.detect(bodies[0]).encoding,
  49. chardet.detect(bodies[1]).encoding,
  50. ]
  51. // We wanted to decode our first stream.
  52. charsets[0].should.equal('ascii');
  53. bodies[0].indexOf('全球中文网站前二十强').should.not.equal(-1);
  54. // But not our second stream.
  55. charsets[1].should.equal('windows-1252');
  56. bodies[1].indexOf('全球中文网站前二十强').should.equal(-1);
  57. done();
  58. });
  59. })
  60. })
  61. })