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
Post a Comment