2017-05-28 8 views
2

firebaseのクラウド機能用のローカルテスト環境をセットアップしようとしています。しかし私は、私のHTTP機能の1つに偽の呼び出しをしようとすると問題に遭遇します。CORSによるfirebase HTTPSコールのテスト

私のエラーの理由は、CORS(npm)を使用しているようです。私がcorsを削除し、response.status(200)だけで以下に示す関数 "test"を実行すると、すべてが動作します。しかし、cors(req、res)でラップすると、私のテストはTypeErrorで失敗します:未定義のプロパティ 'origin'を読み取ることができません。

私はここで間違っていますか? index.jsで

- 私のtest.jsで>

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

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

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

ここでは、その後、あなたのテストでは、あなたの応答をテストすることができsinon

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()); 
}); 

で、私はCORSエラーの周りになった方法は次のとおりです。

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