2012-11-26 26 views
17

मैं एक परीक्षण लिखने की कोशिश कर रहा हूं जो जांचता है कि कोई एपीआई रूट सही सामग्री के साथ ज़िप फ़ाइल आउटपुट करता है या नहीं।node.js सर्वर पर supertest/superagent के साथ प्रतिक्रिया आउटपुट बफर/स्ट्रीम पढ़ें

मैं परीक्षण के लिए मोचा और सुपरटेस्ट का उपयोग कर रहा हूं, और मैं वास्तव में आउटपुट स्ट्रीम/बफर पढ़ना चाहता हूं, ज़िप फ़ाइल सामग्री को पढ़ना चाहता हूं और देख सकता हूं कि सामग्री सही है या नहीं।

कोई विचार मुझे यह कैसे करना चाहिए? जब मैं res.body पढ़ने की कोशिश करता हूं, तो यह केवल एक खाली वस्तु है।

request(app) 
    .get("/api/v1/orders/download?id[]=1&id=2") 
    .set("Authorization", authData) 
    .expect(200) 
    .expect('Content-Type', /application\/zip/) 
    .end(function (err, res) { 
     if (err) return done(err); 

     console.log('body:', res.body) 

     // Write the temp HTML file to filesystem using utf-8 encoding 
     var zip = new AdmZip(res.body); 
     var zipEntries = zip.getEntries(); 

     console.log('zipentries:', zipEntries); 

     zipEntries.forEach(function(zipEntry) { 
     console.log(zipEntry.toString()); // outputs zip entries information 
     }); 

     done(); 
    }); 

उत्तर

1

मुझे लगता है कि आप एप्लिकेशन/ज़िप के लिए अपना स्वयं का पार्सर बनाना चाहते हैं और वास्तविक प्रतिक्रिया डेटा प्राप्त करने के लिए इसका उपयोग करना चाहते हैं; उदाहरण के लिए JSON पार्सर here है। एक बार जब आप इसे प्राप्त कर लेते हैं तो आप इसका अनुरोध करने के लिए इसका उपयोग कर सकते हैं .parse; तो आपका परीक्षण बन जाएगा:

request(app) 
    .get("/api/v1/orders/download?id[]=1&id=2") 
    .set("Authorization", authData) 
    .expect(200) 
    .expect('Content-Type', /application\/zip/) 
    .parse(function (res, fn) { 
    res.data = ''; 
    res.on('data', function (chunk) { res.data += chunk; }); 
    res.on('end', function() { 
     try { 
     fn(null, new AdmZip(res.data)); 
     } catch (err) { 
     fn(err); 
     } 
    }); 
    }) 
    .end(function (err, res) { 
    if (err) return done(err); 

    console.log('body:', res.body) 

    // Write the temp HTML file to filesystem using utf-8 encoding 
    var zipEntries = res.body.getEntries(); 

    console.log('zipentries:', zipEntries); 

    zipEntries.forEach(function(zipEntry) { 
     console.log(zipEntry.toString()); // outputs zip entries information 
    }); 

    done(); 
    }); 

इसका उत्तर खोजने के लिए मैं ज्यादातर अतिसंवेदनशील परीक्षण सूट का निरीक्षण करने पर निर्भर था।

function binaryParser(res, callback) { 
    res.setEncoding('binary'); 
    res.data = ''; 
    res.on('data', function (chunk) { 
     res.data += chunk; 
    }); 
    res.on('end', function() { 
     callback(null, new Buffer(res.data, 'binary')); 
    }); 
} 

// example mocha test 
it('my test', function(done) { 
    request(app) 
     .get('/path/to/image.png') 
     .expect(200) 
     .expect('Content-Type', 'image.png') 
     .buffer() 
     .parse(binaryParser) 
     .end(function(err, res) { 
      if (err) return done(err); 

      // binary response data is in res.body as a buffer 
      assert.ok(Buffer.isBuffer(res.body)); 
      console.log("res=", res.body); 

      done(); 
     }); 
}); 
22

@ Beau के जवाब में विस्तार करते हुए, निम्नलिखित एक बफर जो आप request.end() में आगे की जांच कर सकते रूप में किसी भी बाइनरी प्रतिक्रिया सामग्री प्राप्त करने के लिए इस्तेमाल किया जा सकता। मैं क्या कर रहा था:

// parses response.body buffer into a data object 
const parsePDF = response => { 
    return new Promise((resolve, reject) => { 
    // code that parses response.body as buffer 
    // and calls resolve(data); when done 
    // or reject(err); on error 
    }) 
}; 

const binaryParser = require('superagent-binary-parser'); 

// test snippet 
request(app) 
    .get('/some/api/returning/pdf') 
    .expect(200) 
    .expect('content-type', 'application/pdf') 
    .parse(binaryParser) 
    .buffer() 
    .then(parsePDF) 
    .then((pdf) => { 
     chai.expect(pdf.pages.length).to.be.equal(5); 
    }) 
+1

यह बहुत अच्छा काम करता है, हालांकि मुझे अनुरोध में '.buffer()' जोड़ना पड़ा। – Nate

+0

@ डॉट्स [http://visionmedia.github.io/superagent/#parsing-response-bodies) से @Nate के साथ, "अगर प्रतिक्रिया बफरिंग सक्षम नहीं है (.buffer (false)) तो प्रतिक्रिया घटना होगी शरीर के पार्सर को खत्म करने के इंतजार किए बिना उत्सर्जित रहें, इसलिए प्रतिक्रिया। कोई भी उपलब्ध नहीं होगा "। – ZachB

+0

@ZachB तो '.buffer()। पार्स (बाइनरीपार्स) '? – rcoup

-1

मौजूदा जवाब मेरे लिए काम नहीं किया: :)

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^