c# - How to shim HttpWebRequest Headers? -


i trying shim following code:

httpwebrequest request = (httpwebrequest)webrequest.create(uri); request.method = "get"; request.headers.add("authorization", "bearer " + authtoken.token.access_token); request.accept = "application/json"; 

but running unit test throws exception in part: request.headers.add() because request.headers null. this, in spite of initializing headers in test:

shimhttpwebrequest request = new shimhttpwebrequest(); shimwebrequest.createstring = (urio) => {    request.instance.headers = new webheadercollection {         {"authorization", "bearer abcd1234"}    };    //also tried initilizing this:    //webheadercollection headers = new webheadercollection();    //headers[httprequestheader.authorization] = "bearer abcd1234";    //request.instance.headers = headers;    return request.instance; }; 

but request.instance.headers still null.

what missing?

i solved creating getter headers return webheadercollection instead of null.

shimhttpwebrequest request = new shimhttpwebrequest(); shimwebrequest.createstring = (urio) => request.instance; request.headersget = () => {    webheadercollection headers = new webheadercollection();    headers.add("authorization", "bearer abcd1234");    return headers; }; 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -