2017-05-28 84 views
2

我正在嘗試爲我的firebase雲功能設置本地測試環境。然而,當我嘗試對我的一個HTTP函數進行虛假呼叫時遇到了問題。使用CORS測試firebase HTTPS調用

我錯誤的原因似乎是我使用CORS(npm)。當我刪除cors並運行下面僅顯示response.status(200)的函數「test」時,一切正常。但是,當使用cors(req,res)進行包裝時,我的測試會因TypeError失敗:無法讀取未定義的屬性'origin'。

我在這裏做錯了什麼?

在index.js - >

exports.test = functions.https.onRequest((request, response) => { 

cors(request, response,() => { 
    response.status(200); 
    response.send("test ok"); 
}) 

在我test.js

describe('Cloud Functions',() => { 
    // [START stubConfig] 
    var myFunctions, configStub, adminInitStub, functions, admin, cors; 

    before(() => { 
     // Since index.js makes calls to functions.config and admin.initializeApp at the top of the file, 
     // we need to stub both of these functions before requiring index.js. This is because the 
     // functions will be executed as a part of the require process. 
     // Here we stub admin.initializeApp to be a dummy function that doesn't do anything. 
     admin = require('firebase-admin'); 
     cors = require('cors')({ 
      origin: true 
     }); 
     adminInitStub = sinon.stub(admin, 'initializeApp'); 
     // Next we stub functions.config(). Normally config values are loaded from Cloud Runtime Config; 
     // here we'll just provide some fake values for firebase.databaseURL and firebase.storageBucket 
     // so that an error is not thrown during admin.initializeApp's parameter check 
     functions = require('firebase-functions'); 
     configStub = sinon.stub(functions, 'config').returns({ 
      firebase: { 
       databaseURL: 'https://not-a-project.firebaseio.com', 
       storageBucket: 'not-a-project.appspot.com', 
      } 
      // You can stub any other config values needed by your functions here, for example: 
      // foo: 'bar' 
     }); 
     // Now we can require index.js and save the exports inside a namespace called myFunctions. 
     // This includes our cloud functions, which can now be accessed at myFunctions.makeUppercase 
     // and myFunctions.addMessage 
     myFunctions = require('../index'); 
    }); 

    after(() => { 
     // Restoring our stubs to the original methods. 
     configStub.restore(); 
     adminInitStub.restore(); 
    }); 
    // [END stubConfig] 


     describe('test',() => { 
      it('should return status code 200', (done) => { 

       // [START invokeHTTPS] 
       // A fake request object, with req.query.text set to 'input' 
       const req = {}; 
       // A fake response object, with a stubbed redirect function which asserts that it is called 
       // with parameters 303, 'new_ref'. 
       const res = { 
        status: (status) => { 
         assert.equal(status, 200); 
         done(); 
        } 
       }; 

       // Invoke addMessage with our fake request and response objects. This will cause the 
       // assertions in the response object to be evaluated. 
       myFunctions.test(req, res); 
       // [END invokeHTTPS] 


      }) 
     }) 



}) 

回答

0

添加頁眉:{產地: '*'},您的要求和的setHeader(){}到響應

const req = { 
 
    { origin: '*' } 
 
}; 
 

 
const res = { 
 
    status: (status) => { 
 
     assert.equal(status, 200); 
 
     done(); 
 
    } 
 
};

0

這是我周圍的CORS錯誤是如何得到的,與興農

const res = {}; 

Object.assign(res, { 
    status: sinon.stub().returns(res), 
    end: sinon.stub().returns(res), 
    json: sinon.stub().returns(res), 
    setHeader: sinon.stub(), 
    getHeader: sinon.stub(), 
}); 

beforeEach(() => { 
    Object.values(res).forEach(stub => stub.resetHistory()); 
}); 

然後,在您的測試,可以測試你的反應:

cloudFunctions.testFunction({ query: { text: 'foo' } }, res); 
    response = res.json.lastCall.args[0];