Beispiel #1
0
        public static CommonPayload CreateCommonPayload(AstoriaRequestResponseBase rr)
        {
            if (rr == null)
            {
                throw new ArgumentNullException("rr");
            }

            SerializationFormatKind format;

            if (rr is AstoriaRequest)
            {
                format = (rr as AstoriaRequest).Format;
            }
            else
            {
                format = (rr as AstoriaResponse).Request.Format;
            }

            switch (format)
            {
            case SerializationFormatKind.Atom:
            case SerializationFormatKind.PlainXml:
            case SerializationFormatKind.Default:
                return(new XMLPayload(rr));

            case SerializationFormatKind.JSON:
                return(new JSONPayload(rr));
            }

            throw new Exception("Unrecognized format");
        }
Beispiel #2
0
        // DO NOT INSTANTIATE THIS DIRECTLY. Use either CommonPayload.CreateCommonPayload or AstoriaResponse.CommonPayload
        internal XMLPayload(AstoriaRequestResponseBase rr)
            : base(rr)
        {
            var request = rr as AstoriaRequest;
            var response = rr as AstoriaResponse;

            string uri = null;
            if (request != null)
            {
                uri = request.URI;    
            }
            else if(response != null)
            {
                uri = response.Request.URI;
            }

            // for $value requests, the payload is just a raw string, so do not attempt to parse it as xml
            if (uri != null && uri.EndsWith("$value", StringComparison.Ordinal))
            {
                this.Value = this.RawPayload;
                return;
            }

            ParseResults(this.RawPayload);
        }
Beispiel #3
0
        // DO NOT INSTANTIATE THIS DIRECTLY. Use either CommonPayload.CreateCommonPayload or AstoriaResponse.CommonPayload
        internal XMLPayload(AstoriaRequestResponseBase rr)
            : base(rr)
        {
            var request  = rr as AstoriaRequest;
            var response = rr as AstoriaResponse;

            string uri = null;

            if (request != null)
            {
                uri = request.URI;
            }
            else if (response != null)
            {
                uri = response.Request.URI;
            }

            // for $value requests, the payload is just a raw string, so do not attempt to parse it as xml
            if (uri != null && uri.EndsWith("$value", StringComparison.Ordinal))
            {
                this.Value = this.RawPayload;
                return;
            }

            ParseResults(this.RawPayload);
        }
Beispiel #4
0
 protected CommonPayload(AstoriaRequestResponseBase rr)
 {
     if (rr is AstoriaRequest)
     {
         this.Request     = rr as AstoriaRequest;
         this.Response    = null;
         this.WasResponse = false;
     }
     else
     {
         this.Response    = rr as AstoriaResponse;
         this.Request     = this.Response.Request;
         this.WasResponse = true;
     }
 }
Beispiel #5
0
        // DO NOT INSTANTIATE THIS DIRECTLY. Use either CommonPayload.CreateCommonPayload or AstoriaResponse.CommonPayload
        internal JSONPayload(AstoriaRequestResponseBase rr)
            : base(rr)
        {
            string jsonString = this.RawPayload;
            if (jsonString == null)
            {
                this.Value = null;
                return;
            }

            jsonString = StripSecurityWrapper(jsonString);

            object jsonData = null;
            try
            {
                jsonData = Evaluator.EvalToObject("(" + jsonString + ")");
            }
            catch (Exception e)
            {
                this.Value = jsonString;
                return;
            }

            if (jsonData is ArrayObject)
            {
                this.Resources = ParseJsonArray((ArrayObject)jsonData);
            }
            else if (jsonData is JSObject)
            {
                FieldInfo[] fields = ((JSObject)jsonData).GetFields(BindingFlags.Default);

                // v2-style array, with count/page
                if (fields.Any(field => field.Name == v2JsonResultsField))
                {
                    foreach (FieldInfo field in fields)
                    {
                        object value = field.GetValue(field);
                        switch (field.Name)
                        {
                            case v2NextField:
                                this.NextLink = value.ToString();
                                break;

                            case v2CountField:
                                this.Count = Convert.ToInt64(value.ToString());
                                break;

                            case v2JsonResultsField:
                                if (value is ArrayObject)
                                    this.Resources = ParseJsonArray((ArrayObject)value);
                                else
                                    AstoriaTestLog.FailAndThrow("Array expected in results field");
                                break;

                            default:
                                AstoriaTestLog.FailAndThrow("Field '" + field.Name + "' unexpected");
                                break;
                        }
                    }
                }
                else if (fields.Any(field => field.Name == "__metadata"))
                {
                    this.Resources = ParseSingleJsonObject((JSObject)jsonData);   // entry
                }
                else
                {
                    if (fields.Length > 1)
                        AstoriaTestLog.FailAndThrow("Single field expected for non-entry payload");

                    FieldInfo field = fields[0];
                    object value = field.GetValue(field);

                    PayloadObject payloadObject = new PayloadObject(this);
                    PayloadProperty property;
                    if (value is JSObject)
                        property = parseComplexObject(payloadObject, (JSField)field);
                    else
                        property = parseSimpleObject(payloadObject, (JSField)field);

                    if (property.Name != "uri")
                        this.Resources = property;
                    else
                    {
                        // this is to match the resulting structure for parsing links in ATOM / json collections
                        PayloadObject temp = new PayloadObject(this);
                        temp.Name = property.Name;
                        temp.PayloadProperties.Add(property);
                        this.Resources = new List<PayloadObject>() { temp };
                    }
                }
            }
            else
            {
                this.Value = jsonString; // must be $value
            }
        }
Beispiel #6
0
        // DO NOT INSTANTIATE THIS DIRECTLY. Use either CommonPayload.CreateCommonPayload or AstoriaResponse.CommonPayload
        internal JSONPayload(AstoriaRequestResponseBase rr)
            : base(rr)
        {
            string jsonString = this.RawPayload;

            if (jsonString == null)
            {
                this.Value = null;
                return;
            }

            jsonString = StripSecurityWrapper(jsonString);

            object jsonData = null;

            try
            {
                jsonData = Evaluator.EvalToObject("(" + jsonString + ")");
            }
            catch (Exception e)
            {
                this.Value = jsonString;
                return;
            }

            if (jsonData is ArrayObject)
            {
                this.Resources = ParseJsonArray((ArrayObject)jsonData);
            }
            else if (jsonData is JSObject)
            {
                FieldInfo[] fields = ((JSObject)jsonData).GetFields(BindingFlags.Default);

                // v2-style array, with count/page
                if (fields.Any(field => field.Name == v2JsonResultsField))
                {
                    foreach (FieldInfo field in fields)
                    {
                        object value = field.GetValue(field);
                        switch (field.Name)
                        {
                        case v2NextField:
                            this.NextLink = value.ToString();
                            break;

                        case v2CountField:
                            this.Count = Convert.ToInt64(value.ToString());
                            break;

                        case v2JsonResultsField:
                            if (value is ArrayObject)
                            {
                                this.Resources = ParseJsonArray((ArrayObject)value);
                            }
                            else
                            {
                                AstoriaTestLog.FailAndThrow("Array expected in results field");
                            }
                            break;

                        default:
                            AstoriaTestLog.FailAndThrow("Field '" + field.Name + "' unexpected");
                            break;
                        }
                    }
                }
                else if (fields.Any(field => field.Name == "__metadata"))
                {
                    this.Resources = ParseSingleJsonObject((JSObject)jsonData);   // entry
                }
                else
                {
                    if (fields.Length > 1)
                    {
                        AstoriaTestLog.FailAndThrow("Single field expected for non-entry payload");
                    }

                    FieldInfo field = fields[0];
                    object    value = field.GetValue(field);

                    PayloadObject   payloadObject = new PayloadObject(this);
                    PayloadProperty property;
                    if (value is JSObject)
                    {
                        property = parseComplexObject(payloadObject, (JSField)field);
                    }
                    else
                    {
                        property = parseSimpleObject(payloadObject, (JSField)field);
                    }

                    if (property.Name != "uri")
                    {
                        this.Resources = property;
                    }
                    else
                    {
                        // this is to match the resulting structure for parsing links in ATOM / json collections
                        PayloadObject temp = new PayloadObject(this);
                        temp.Name = property.Name;
                        temp.PayloadProperties.Add(property);
                        this.Resources = new List <PayloadObject>()
                        {
                            temp
                        };
                    }
                }
            }
            else
            {
                this.Value = jsonString; // must be $value
            }
        }