Пример #1
0
 private void ChangeAcceptHeader(object sender, SendingRequest2EventArgs e)
 {
     if (e.RequestMessage.GetHeader("content-type") != null)
     {
         e.RequestMessage.SetHeader("content-type", _acceptHeader);
     }
     e.RequestMessage.SetHeader("accept", _acceptHeader);
 }
            internal void VerifySendingRequest(object sender, SendingRequest2EventArgs args)
            {
                this.assert.AreSame(this.expectedContext, sender, "Sending request fired by unexpected context");
                this.assert.IsNotNull(args, "args");

                this.httpTracker.TryCompleteCurrentRequest(this.expectedContext);

                this.assert.IsNotNull(args.RequestMessage, "args.RequestMessage");

                if (args.RequestMessage.Method == "GET")
                {
                    // Special case: GetReadStream gives a StreamDescriptor
                    if (!(args.Descriptor is StreamDescriptor))
                    {
                        this.assert.IsNull(args.Descriptor, "Descriptor should be null for get requests, except GetReadStream");
                    }
                }
                else
                {
                    // Special case : The batch request itself has the method POST yet there's no descriptor for it.
                    if (args.RequestMessage.Url.OriginalString.Contains("/" + Endpoints.Batch))
                    {
                        this.assert.IsNull(args.Descriptor, "Descriptor should be null for batch requests");
                    }
                    else if (this.IsInvokeOperationRequest(args.RequestMessage.Url.OriginalString))
                    {
                        this.assert.IsNull(args.Descriptor, "Descriptor should be null for action or WebInvoke requests");
                    }
                    else
                    {
                        this.assert.IsNotNull(args.Descriptor, "Descriptor should not be null for CUD requests");
                    }
                }

                if (args.IsBatchPart)
                {
                    var requestMessage = args.RequestMessage;
                    this.assert.IsNotNull(requestMessage, "RequestMessage should be of type IODataRequestMessage");
                }
                else
                {
                    if (args.RequestMessage.GetType().Name != "InternalODataRequestMessage")
                    {
                        var requestMessage = args.RequestMessage as HttpWebRequestMessage;

                        this.assert.IsNotNull(requestMessage, "RequestMessage should be of type HttpWebRequestMessage");

                        var httpWebRequest = requestMessage.HttpWebRequest as HttpWebRequest;
                        this.assert.IsNotNull(httpWebRequest, "RequestMessage.HttpWebRequest should be of type HttpWebRequest");

#if WIN8
                    if (this.expectedContext.Credentials != null)
                    {
                        this.assert.AreSame(this.expectedContext.Credentials, httpWebRequest.Credentials, "Request credentials were not set");
                    }
#else
                        this.assert.AreSame(this.expectedContext.Credentials, httpWebRequest.Credentials, "Request credentials were not set");
#endif

                        this.assert.IsNull(this.uriFromEvent, "Last uri unexpectedly not null. Test hook did not fire");
                        this.assert.AreEqual(requestMessage.Url, httpWebRequest.RequestUri, "Request Uri does not match RequestUri from HttpWebRequest");
                        this.uriFromEvent = httpWebRequest.RequestUri;

                        this.assert.IsNull(this.methodFromEvent, "Last method unexpectedly not null. Test hook did not fire");
                        this.methodFromEvent = httpWebRequest.Method;

                        this.assert.IsNull(this.originalHeadersFromEvent, "Last headers unexpectedly not null. Test hook did not fire");
                        this.assert.AreEqual(requestMessage.Headers.Count(), httpWebRequest.Headers.Count, "Request Headers count does not match Headers count from HttpWebRequest");

                        this.originalHeadersFromEvent = requestMessage.Headers.ToDictionary(k => k.Key, v => v.Value);
                        this.expectedHeaders = new Dictionary<string, string>(this.originalHeadersFromEvent);

                        if (this.originalHeadersFromEvent.ContainsKey("X-HTTP-Method"))
                        {
                            this.methodFromEvent = this.originalHeadersFromEvent["X-HTTP-Method"];
                        }

                        if (this.callback != null)
                        {
                            this.callback(args, this.expectedHeaders);
                        }

                        this.finalHeadersFromEvent = requestMessage.Headers.ToDictionary(k => k.Key, v => v.Value);
                    }
                    else
                    {
                        this.uriFromEvent = args.RequestMessage.Url;
                        this.methodFromEvent = args.RequestMessage.Method;
                        this.originalHeadersFromEvent = args.RequestMessage.Headers.ToDictionary(k => k.Key, v => v.Value);

                        if (this.originalHeadersFromEvent.ContainsKey("X-HTTP-Method"))
                        {
                           this.methodFromEvent = this.originalHeadersFromEvent["X-HTTP-Method"];
                        }

                        this.expectedHeaders = new Dictionary<string, string>(this.originalHeadersFromEvent);

                        if (this.callback != null)
                        {
                            this.callback(args, this.expectedHeaders);
                        }

                        this.finalHeadersFromEvent = args.RequestMessage.Headers.ToDictionary(k => k.Key, v => v.Value);
                    }
                }
            }
 /// <summary>
 /// Raises the AlterRequestAndExpectedHeaders event if there are any consumers
 /// </summary>
 /// <param name="args">The sending request args</param>
 /// <param name="expectedHeaders">The expected headers</param>
 internal void RaiseEvent(SendingRequest2EventArgs args, IDictionary<string, string> expectedHeaders)
 {
     if (this.AlterRequestAndExpectedHeaders != null)
     {
         this.AlterRequestAndExpectedHeaders(args, expectedHeaders);
     }
 }
Пример #4
0
 /// <summary>
 /// Fires the SendingRequest2 event.
 /// </summary>
 /// <param name="eventArgs">SendingRequest2EventArgs instance containing all information about the request.</param>
 internal void FireSendingRequest2(SendingRequest2EventArgs eventArgs)
 {
     this.Context.FireSendingRequest2(eventArgs);
 }
Пример #5
0
 private void Container_SendingRequest2(object sender, SendingRequest2EventArgs e)
 {
     Console.WriteLine("\t{0} {1}", e.RequestMessage.Method, e.RequestMessage.Url.ToString());
 }
Пример #6
0
 void Container_SendingRequest(object sender, SendingRequest2EventArgs e)
 {
     Console.WriteLine("{0} {1}", e.RequestMessage.Method, e.RequestMessage.Url);
 }
 // use SendingRequest2/SendingRequest2EventArgs since the client does not allow SendingRequest and BuildingRequest registered at the same time
 private void InjectAuthenticationCookies(object sender, SendingRequest2EventArgs e)
 {
     if (this.authenticationHeaders != null)
     {
         foreach (var header in this.authenticationHeaders)
         {
             e.RequestMessage.SetHeader(header.Key, header.Value);
         }
     }
 }
 private static void  SetInvalidETag(object obj, SendingRequest2EventArgs args)
 {
     args.RequestMessage.SetHeader("If-Match", "W/\"var1\""); 
 }
 private static void  SetInvalidDataServiceVersion(object obj, SendingRequest2EventArgs args)
 {
     args.RequestMessage.SetHeader("DataServiceVersion", "99.99;NetFx");
 }