public void CheckNonEmptyParams() { string key = "hello"; string missingKey = "blah"; string value = "there"; string desc = "the hello key"; string form = string.Format("{0}={1}", key, value); Stream sampleForm = new MemoryStream(Encoding.UTF8.GetBytes(form)); FormBodyParser fb = new FormBodyParser(sampleForm); // // See if keys that should be there work // Dictionary <string, string> keys = new Dictionary <string, string>() { { key, desc } }; string res = fb.CheckNonEmptyParams(keys, null); Assert.IsTrue(res == string.Empty); // // See if keys that shouldn't be there are identified // Dictionary <string, string> badKeys = new Dictionary <string, string>() { { missingKey, desc } }; string badRes = fb.CheckNonEmptyParams(badKeys, null); Assert.AreEqual(desc, badRes); }
private errordetail ValidateParameters(FormBodyParser form) { errordetail result = null; // @"soundId=%@&rating=%d&text=%@" if (!Functions.IsNumeric(form.Value(QsKeys.SoundId))) { result = new errordetail("Value for soundid must be numeric.", System.Net.HttpStatusCode.BadRequest); } else if (!Functions.IsNumeric(form.Value(QsKeys.Rating))) { result = new errordetail("Value for rating must be numeric.", System.Net.HttpStatusCode.BadRequest); } else { int rating = Functions.ConvertInt(form.Value(QsKeys.Rating), -1); if (rating <= 0 || rating >= 6) { result = new errordetail("Value for rating must be 1 through 5.", System.Net.HttpStatusCode.BadRequest); } } return(result); }
private status RecordStorePurchase(Stream postBody) { // // Validate the request. // RequestValidation.Validate(); // // Get the passed data POSTed to the service as a form. // FormBodyParser form = new FormBodyParser(postBody); errordetail validationError = ValidateParameters(form); if (validationError != null) { throw new WebFaultException <errordetail>(validationError, validationError.statuscode); } // // With the passed values, let's make it so. // bool res = DataManager.RecordPurchase(form.Value(QsKeys.PurchaseId), form.Value(QsKeys.DeviceId), form.Value(QsKeys.AppVersion)); if (res) { return(new status(ResultStatus.Success)); } else { return(new status(ResultStatus.Error)); } }
public void Instantiate() { string form = "hello=there"; Stream sampleForm = new MemoryStream(Encoding.UTF8.GetBytes(form)); FormBodyParser fb = new FormBodyParser(sampleForm); Assert.IsNotNull(fb); }
public void ParseAForm() { string form = "hello=there"; Stream sampleForm = new MemoryStream(Encoding.UTF8.GetBytes(form)); FormBodyParser fb = new FormBodyParser(sampleForm); Assert.IsNotNull(fb.Value("hello")); Assert.IsTrue(fb.Value("uggabugga") == string.Empty); }
/// <summary> /// Validate the passed form for values /// </summary> /// <param name="form"></param> /// <returns></returns> private errordetail ValidateParameters(FormBodyParser form) { errordetail result = null; if (!Functions.IsNumeric(form.Value(QsKeys.SoundId))) { result = new errordetail("Value for soundid must be numeric.", System.Net.HttpStatusCode.BadRequest); } return(result); }
public void CheckMaxStringLengths() { string key = "hello"; string value = "my value for this key"; int valLen = value.Length; string form = string.Format("{0}={1}", key, value); Stream sampleForm = new MemoryStream(Encoding.UTF8.GetBytes(form)); FormBodyParser fb = new FormBodyParser(sampleForm); Dictionary <string, int> keys = new Dictionary <string, int>() { { key, valLen + 1 } }; string res = fb.CheckMaxStringLengths(keys); Assert.IsTrue(res == string.Empty, "Max len is greater than string, should be ok"); Dictionary <string, int> keys2 = new Dictionary <string, int>() { { key, valLen } }; res = fb.CheckMaxStringLengths(keys2); Assert.IsTrue(res == string.Empty, "Max len equals string len, should be ok"); Dictionary <string, int> keys3 = new Dictionary <string, int>() { { key, valLen - 1 } }; res = fb.CheckMaxStringLengths(keys3); Assert.IsTrue(res != string.Empty, "String too long, should be rejected"); Assert.IsTrue(res.Contains(key), "Return message should contain the parameter name"); Dictionary <string, int> keys4 = new Dictionary <string, int>() { { "bugga", valLen + 1 } }; res = fb.CheckMaxStringLengths(keys4); Assert.IsTrue(res == string.Empty, "Non-existant string should be ok"); }
/// <summary> /// Process the upload request. /// </summary> /// <param name="postBody">The upload data POSTed to the service</param> /// <returns>The inserted sound if successful, otherwise throws an eror. Just the new id is populated.</returns> private sound UploadASound(Stream postBody) { // // Validate the request. // RequestValidation.Validate(); // // Get the passed data POSTed to the service as a form. // FormBodyParser form = new FormBodyParser(postBody); errordetail validationError = ValidateParameters(form); if (validationError != null) { throw new WebFaultException <errordetail>(validationError, validationError.statuscode); } // // With the passed values, let's make it so. // int newId = -1; bool res = DataManager.InsertSound(form.Value(QsKeys.Name), form.Value(QsKeys.SoundFName), form.Value(QsKeys.Description), form.Value(QsKeys.UserId), form.Base64DecodedValue(QsKeys.SoundData), form.Value(QsKeys.IconFName), form.Base64DecodedValue(QsKeys.IconData), Functions.ConvertBool(form.Value(QsKeys.IsBrowsable), false), out newId ); //bool res = false; if (!res) { throw new WebFaultException <errordetail>(new errordetail("Error saving sound", System.Net.HttpStatusCode.InternalServerError), System.Net.HttpStatusCode.InternalServerError); } sound newSound = new sound(); newSound.soundid = newId; return(newSound); }
/// <summary> /// Validate the passed form for values /// </summary> /// <param name="form">The passed data in a form parser</param> /// <returns>The error that happened, or null.</returns> private errordetail ValidateParameters(FormBodyParser form) { errordetail result = null; string missingValue = form.CheckNonEmptyParams(new Dictionary <string, string>() { { QsKeys.Name, "sound name" }, { QsKeys.SoundFName, "sound filename" }, { QsKeys.Description, "sound description" }, { QsKeys.UserId, "user name" }, { QsKeys.SoundData, "sound data" }, { QsKeys.SoundDataMd5, "sound checksum" }, { QsKeys.IconFName, "icon filename" }, { QsKeys.IconData, "icon data" }, { QsKeys.IconDataMd5, "icon checksum" }, { QsKeys.IsBrowsable, "is browsable" } }, form.CommonEmptyValues); if (!Functions.IsEmptyString(missingValue)) { result = new errordetail(string.Format("Value for {0} is missing.", missingValue), System.Net.HttpStatusCode.BadRequest); } else { byte[] soundData = form.Base64DecodedValue(QsKeys.SoundData); byte[] iconData = form.Base64DecodedValue(QsKeys.IconData); if (soundData == null || Functions.GetMd5Hash(soundData) != form.Value(QsKeys.SoundDataMd5)) { result = new errordetail(string.Format("Sound data not received properly."), System.Net.HttpStatusCode.BadRequest); } else if (iconData == null || Functions.GetMd5Hash(iconData) != form.Value(QsKeys.IconDataMd5)) { result = new errordetail(string.Format("Icon data not received properly."), System.Net.HttpStatusCode.BadRequest); } } // // Note: we could check for max value lengths here, but eh, let the stored proc bomb out. The client // should check for lengths // return(result); }
/// <summary> /// Validate the passed form for values /// </summary> /// <param name="form"></param> /// <returns></returns> private errordetail ValidateParameters(FormBodyParser form) { errordetail result = null; // form.Value("purchaseId"), form.Value("userId") if (Functions.IsEmptyString(form.Value(QsKeys.PurchaseId))) { result = new errordetail("Value for purchase id is missing.", System.Net.HttpStatusCode.BadRequest); } else if (Functions.IsEmptyString(form.Value(QsKeys.DeviceId))) { result = new errordetail("Value for device id is missing.", System.Net.HttpStatusCode.BadRequest); } else if (Functions.IsEmptyString(form.Value(QsKeys.AppVersion))) { result = new errordetail("Value for app version is missing.", System.Net.HttpStatusCode.BadRequest); } return(result); }
public void CheckAppleNullParams() { string key = "hello"; string desc = "the hello key"; string appleNull = "(null)"; string form = string.Format("{0}={1}", key, appleNull); Stream sampleForm = new MemoryStream(Encoding.UTF8.GetBytes(form)); FormBodyParser fb = new FormBodyParser(sampleForm); // // See if keys that should be there work // Dictionary <string, string> keys = new Dictionary <string, string>() { { key, desc } }; string res = fb.CheckNonEmptyParams(keys, null); Assert.IsTrue(res == string.Empty); // // See if the passed list of values is properly checked // res = fb.CheckNonEmptyParams(keys, new List <string>() { appleNull }); Assert.IsFalse(res == string.Empty); // // See if the built-in "FormBodyParser" property catches the null as well // res = fb.CheckNonEmptyParams(keys, fb.CommonEmptyValues); Assert.IsFalse(res == string.Empty); }
/// <summary> /// Process the web request /// </summary> /// <param name="postBody">The request parameters</param> /// <returns>The status or an error is thrown.</returns> private status ProcessSoundRating(Stream postBody) { // // Validate the request. // RequestValidation.Validate(); // // Get the passed data POSTed to the service as a form. // FormBodyParser form = new FormBodyParser(postBody); errordetail validationError = ValidateParameters(form); if (validationError != null) { throw new WebFaultException <errordetail>(validationError, validationError.statuscode); } // // With the passed values, let's make it so. // bool res = DataManager.RateSound(int.Parse(form.Value(QsKeys.SoundId)), int.Parse(form.Value(QsKeys.Rating)), form.Value(QsKeys.Text)); return(new status { code = 0, description = "Success" }); /* * else * { * errordetail err = new errordetail("Data update failed.", System.Net.HttpStatusCode.InternalServerError); * throw new WebFaultException<errordetail>(err, err.statuscode); * } * */ }