﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Data.OData;
using Xunit;

namespace ODataV3CoreClr
{
    public class MessageWriter
    {
        [Fact]
        public void Write()
        {
            // Arrange & Act
            var msg = (IODataResponseMessage)new Message();
            var writer = new ODataMessageWriter(msg);
            writer.WriteValue("abc");
            var stream = msg.GetStream();
            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);
            var str = reader.ReadToEnd();

            // Assert
            Assert.Contains("abc", str);
        }
    }

    public class Message : IODataRequestMessage, IODataResponseMessage
    {
        private readonly IDictionary<string, string> headers = new Dictionary<string, string>();
        readonly Stream stream = new MemoryStream();

        public string GetHeader(string headerName)
        {
            string val;
            return headers.TryGetValue(headerName, out val) ? val : string.Empty;
        }

        public void SetHeader(string headerName, string headerValue)
        {
            headers[headerName] = headerValue;
        }

        public Stream GetStream()
        {
            return stream;
        }

        public IEnumerable<KeyValuePair<string, string>> Headers
        {
            get { return headers; }
        }

        public int StatusCode { get; set; }

        public Uri Url { get; set; }

        public string Method { get; set; }
    }
}