From 1ed77371c02657c6871ea8e131a9d5b3c84c6c24 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Sun, 25 Aug 2013 15:07:07 +0800
Subject: [PATCH] Enable customing mime type and charset when returning reqeust
 string job.

---
 browser/api/lib/protocol.coffee | 10 ++++++++++
 spec/api/protocol.coffee        | 15 +++++++++++++--
 spec/main.js                    |  4 ++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/browser/api/lib/protocol.coffee b/browser/api/lib/protocol.coffee
index dadc17e32452..350a3d609748 100644
--- a/browser/api/lib/protocol.coffee
+++ b/browser/api/lib/protocol.coffee
@@ -1 +1,11 @@
 module.exports = process.atomBinding 'protocol'
+
+module.exports.RequestStringJob =
+class RequestStringJob
+  constructor: ({mimeType, charset, data}) ->
+    if typeof data isnt 'string' and not data instanceof Buffer
+      throw new TypeError('Data should be string or Buffer')
+
+    @mimeType = mimeType ? 'text/plain'
+    @charset = charset ? 'UTF-8'
+    @data = String data
diff --git a/spec/api/protocol.coffee b/spec/api/protocol.coffee
index 5be7b4a46875..6f20fcf4454b 100644
--- a/spec/api/protocol.coffee
+++ b/spec/api/protocol.coffee
@@ -11,7 +11,9 @@ describe 'protocol API', ->
       protocol.unregisterProtocol 'test1'
 
     it 'calls the callback when scheme is visited', (done) ->
-      protocol.registerProtocol 'test2', -> done()
+      protocol.registerProtocol 'test2', (url) ->
+        assert.equal url, 'test2://test2'
+        done()
       $.get 'test2://test2', ->
       protocol.unregisterProtocol 'test2'
 
@@ -28,4 +30,13 @@ describe 'protocol API', ->
           assert.equal data, 'atom-string://something'
           done()
         error: (xhr, errorType, error) ->
-          console.log xhr, errorType, error
+          assert false, 'Got error: ' + errorType + ' ' + error
+
+    it 'returns RequestStringJob should send string', (done) ->
+      $.ajax
+        url: 'atom-string-job://something'
+        success: (data) ->
+          assert.equal data, 'atom-string-job://something'
+          done()
+        error: (xhr, errorType, error) ->
+          assert false, 'Got error: ' + errorType + ' ' + error
diff --git a/spec/main.js b/spec/main.js
index ba05d6fdd0b3..851f60817796 100644
--- a/spec/main.js
+++ b/spec/main.js
@@ -41,6 +41,10 @@ app.on('will-finish-launching', function() {
   protocol.registerProtocol('atom-string', function(url) {
     return url;
   });
+
+  protocol.registerProtocol('atom-string-job', function(url) {
+    return new protocol.RequestStringJob({mimeType: 'text/html', data: url});
+  });
 });
 
 app.on('finish-launching', function() {