public void Charset1251() { var part = new Multipart.Part("field"); part.FileName = "text"; part.ContentType = "Content-type: text/plain; charset=windows-1251"; part.Content = Encoding.GetEncoding(1251).GetBytes("Значение"); var mpE = new Multipart(new Multipart.Part[] {part}); var enc = mpE.Encode(); var boundary = enc.Boundary; var mpD = Multipart.ReadFromBytes(enc.Buffer, ref boundary); Assert.AreEqual("Значение", mpD.Parts["field"].Content); }
public void EncodeDecode_FieldAndFile() { var partField = getDefaultField(); var partFile = getDefaultFile(); var mpE = new Multipart(new Multipart.Part[] { partField, partFile }); var encCont = mpE.Encode(); string boundary = null; var mpD = Multipart.ReadFromBytes(encCont.Buffer, ref boundary); Assert.AreEqual(encCont.Boundary, boundary); Assert.AreEqual(2, mpD.Parts.Count); Assert.AreEqual(partField.Content.AsString(), mpD.Parts[partField.Name].Content.AsString()); Assert.IsNull(mpD.Parts[partField.Name].ContentType); Assert.IsNull(mpD.Parts[partField.Name].FileName); Assert.AreEqual(partFile.FileName, mpD.Parts[partFile.Name].FileName); Assert.AreEqual(partFile.ContentType, mpD.Parts[partFile.Name].ContentType); Assert.IsTrue(IOMiscUtils.MemBufferEquals(partFile.Content as byte[], mpD.Parts[partFile.Name].Content as byte[])); }
public void TryCreateMultipart_NullParts() { try { var part = new Multipart(null); Assert.Fail("Invalid parts!"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains(NFX.Web.StringConsts.MULTIPART_PARTS_COULDNT_BE_EMPTY_ERROR)); } }
public void EncodeToStreamDecode() { var partField = getDefaultField(); var partFile = getDefaultFile(); var stream = new MemoryStream(); stream.WriteByte(0xFF); stream.WriteByte(0xFF); var mpE = new Multipart(new Multipart.Part[] { partField, partFile }); var enc = mpE.Encode(stream); Assert.AreEqual(enc.StartIdx, 2); var src = new byte[enc.Length]; Array.Copy(enc.Buffer, enc.StartIdx, src, 0, src.Length); string boundary = null; var mpD = Multipart.ReadFromBytes(src, ref boundary); Assert.AreEqual(enc.Boundary, boundary); Assert.AreEqual(partField.Content.AsString(), mpD.Parts[partField.Name].Content.AsString()); Assert.IsNull(mpD.Parts[partField.Name].ContentType); Assert.IsNull(mpD.Parts[partField.Name].FileName); Assert.AreEqual(partFile.FileName, mpD.Parts[partFile.Name].FileName); Assert.AreEqual(partFile.ContentType, mpD.Parts[partFile.Name].ContentType); Assert.IsTrue(IOMiscUtils.MemBufferEquals(partFile.Content as byte[], mpD.Parts[partFile.Name].Content as byte[])); }
public void FieldAndFile_ToJSONDataMap() { var partField = getDefaultField(); var partFile = getDefaultFile(); var mp = new Multipart(new Multipart.Part[] { partField, partFile }); Assert.AreEqual(2, mp.Parts.Count); var map = mp.ToJSONDataMap(); Assert.AreEqual(partField.Content.AsString(), map[partField.Name].AsString()); Assert.AreEqual(partFile.FileName, map[partFile.Name + POSTFIX_FILENAME].AsString()); Assert.AreEqual(partFile.ContentType, map[partFile.Name + POSTFIX_CONTENT_TYPE].AsString()); Assert.IsTrue(IOMiscUtils.MemBufferEquals(partFile.Content as byte[], map[partFile.Name] as byte[])); }
public void EncodeDecode_OneFile() { var part = getDefaultFile(); var mpE = new Multipart(new Multipart.Part[] { part }); var encCont = mpE.Encode(); string boundary = null; var mpD = Multipart.ReadFromBytes(encCont.Buffer, ref boundary); Assert.AreEqual(encCont.Boundary, boundary); Assert.AreEqual(1, mpD.Parts.Count); Assert.IsTrue(IOMiscUtils.MemBufferEquals(part.Content as byte[], mpD.Parts[part.Name].Content as byte[])); Assert.AreEqual(part.FileName, mpD.Parts[part.Name].FileName); Assert.AreEqual(part.ContentType, mpD.Parts[part.Name].ContentType); }
public void EncodeDecode_OneField_Enc1251() { var encoding = Encoding.GetEncoding(1251); var part = new Multipart.Part("Field"); part.Content = "Значение"; var mpE = new Multipart(new Multipart.Part[] { part }); var encCont = mpE.Encode(encoding); string boundary = null; var mpD = Multipart.ReadFromBytes(encCont.Buffer, ref boundary, encoding); Assert.AreEqual(encCont.Boundary, boundary); Assert.AreEqual(encCont.Encoding, encoding); Assert.AreEqual(1, mpD.Parts.Count); Assert.AreEqual(part.Content.AsString(), mpD.Parts[part.Name].Content.AsString()); Assert.IsNull(mpD.Parts[part.Name].ContentType); Assert.IsNull(mpD.Parts[part.Name].FileName); }
public void EncodeDecode_OneField() { var part = getDefaultField(); var mpE = new Multipart(new Multipart.Part[] { part }); var encCont = mpE.Encode(); string boundary = null; var mpD = Multipart.ReadFromBytes(encCont.Buffer, ref boundary); Assert.AreEqual(encCont.Boundary, boundary); Assert.AreEqual(1, mpD.Parts.Count); Assert.AreEqual(part.Content.AsString(), mpD.Parts[part.Name].Content.AsString()); Assert.IsNull(mpD.Parts[part.Name].ContentType); Assert.IsNull(mpD.Parts[part.Name].FileName); }
public static Multipart ReadFromBytes(byte[] buffer, ref string boundary, Encoding encoding = null) { var stream = new MemoryStream(buffer); return(Multipart.ReadFromStream(stream, ref boundary, encoding)); }
public static Multipart ReadFromStream(Stream stream, ref string boundary, Encoding encoding = null) { if (stream == null || !stream.CanRead) throw new NFXException(StringConsts.MULTIPART_STREAM_NOT_NULL_MUST_SUPPORT_READ_ERROR); encoding = encoding ?? Encoding.UTF8; // find boundary var boundaryBytes = boundary.IsNullOrWhiteSpace() ? findBoundary(stream, encoding, out boundary) : locateBoundary(stream, encoding, boundary); var result = new Multipart(); // fill parts from stream foreach (var segment in streamSplitNew(stream, boundaryBytes)) { var part = partParse(segment.Array, segment.Count, encoding); var success = result.Parts.Register(part); if (!success) throw new NFXException(StringConsts.MULTIPART_PART_IS_ALREADY_REGISTERED_ERROR.Args(part.Name)); } return result; }