Skip to content

Commit

Permalink
Merge branch 'kolosy', and cleaned up postStream in CouchRequest.
Browse files Browse the repository at this point in the history
  • Loading branch information
gokr committed Dec 1, 2009
2 parents c72aa7d + 0ad1376 commit 1e0e519
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Tests/CouchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void ShouldSaveDocumentWithoutId()

private class LittleCar
{
public string Id, Rev;
public string _id, _rev;
public string Make, Model, docType;
}

Expand All @@ -214,17 +214,17 @@ public void ShouldSaveArbitraryDocument()
{
var littleCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" };
littleCar = db.SaveArbitraryDocument(littleCar);
Assert.IsNotNull(littleCar.Id);
Assert.IsNotNull(littleCar._id);
}

[Test]
public void ShouldLoadArbitraryDocument()
{
var firstCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" };
firstCar = db.SaveArbitraryDocument(firstCar);
var otherCar = db.GetArbitraryDocument<LittleCar>(firstCar.Id, () => new LittleCar());
var otherCar = db.GetArbitraryDocument<LittleCar>(firstCar._id, () => new LittleCar());
Assert.IsNotNull(otherCar);
Assert.IsNotNull(otherCar.Id);
Assert.IsNotNull(otherCar._id);
}

[Test]
Expand Down
3 changes: 0 additions & 3 deletions Tests/Divan.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@
<Name>Divan</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
2 changes: 1 addition & 1 deletion src/CouchDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public ICouchDocument SaveDocument(ICouchDocument document)
else
savedDoc = WriteDocument(document);
}
catch (CouchConflictException ex)
catch (CouchConflictException)
{
if (reconcilingDoc == null)
throw;
Expand Down
18 changes: 9 additions & 9 deletions src/CouchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class CouchRequest
public string method = "GET"; // PUT, DELETE, POST, HEAD
public string mimeType;
public string path;
//public byte[] postData;
public string query;

public JToken result;
Expand Down Expand Up @@ -134,22 +133,18 @@ public CouchRequest Delete()

public CouchRequest Data(string data)
{
var postData = Encoding.UTF8.GetBytes(data);
return Data(postData);
//this.postStream = new MemoryStream(postData);
//return this;
return Data(Encoding.UTF8.GetBytes(data));
}

public CouchRequest Data(byte[] data)
{
this.postStream = new MemoryStream(data);
//postData = data;
postStream = new MemoryStream(data);
return this;
}

public CouchRequest Data(Stream dataStream)
{
this.postStream = dataStream;
postStream = dataStream;
return this;
}

Expand Down Expand Up @@ -202,7 +197,7 @@ public CouchRequest Check(string message)

private HttpWebRequest GetRequest()
{
Uri requestUri = new UriBuilder("http", server.Host, server.Port, ((db != null) ? db.Name + "/" : "") + path, query).Uri;
var requestUri = new UriBuilder("http", server.Host, server.Port, ((db != null) ? db.Name + "/" : "") + path, query).Uri;
var request = WebRequest.Create(requestUri) as HttpWebRequest;
if (request == null)
{
Expand All @@ -221,6 +216,11 @@ private HttpWebRequest GetRequest()
request.Headers.Add(header.Key, header.Value);
}

if (!string.IsNullOrEmpty(server.EncodedCredentials))
{
request.Headers.Add("Authorization", server.EncodedCredentials);
}

if (postStream != null)
{
WriteData(request);
Expand Down
17 changes: 16 additions & 1 deletion src/CouchServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

Expand All @@ -25,16 +26,30 @@ public class CouchServer
public readonly string Host;
public readonly int Port;

public readonly string UserName;
public readonly string Password;
public readonly string EncodedCredentials;

public string DatabasePrefix = ""; // Used by databases to prefix their names

public CouchServer(string host, int port)
public CouchServer(string host, int port, string user, string pass)
{
Host = host;
Port = port;
UserName = user;
Password = pass;

if (!String.IsNullOrEmpty(UserName))
EncodedCredentials = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(UserName + ":" + Password));

Debug(string.Format("CouchServer({0}:{1})", host, port));
}

public CouchServer(string host, int port): this(host, port, null, null)
{
}

public CouchServer(string host)
: this(host, DefaultPort)
{
Expand Down

0 comments on commit 1e0e519

Please sign in to comment.