1- using CouchDB . Driver . Types ;
1+ using CouchDB . Driver . Exceptions ;
2+ using CouchDB . Driver . Types ;
23using CouchDB . Driver . UnitTests . Models ;
34using Flurl . Http . Testing ;
45using System . Collections . Generic ;
6+ using System . Linq ;
7+ using System . Net ;
58using System . Net . Http ;
69using System . Threading . Tasks ;
710using Xunit ;
@@ -110,7 +113,7 @@ public async Task IsUp()
110113
111114 using ( var client = new CouchClient ( "http://localhost" ) )
112115 {
113- var result = await client . IsUpAsync ( ) ;
116+ var result = await client . IsUpAsync ( ) ;
114117 Assert . True ( result ) ;
115118 }
116119 }
@@ -127,7 +130,7 @@ public async Task IsNotUp()
127130
128131 using ( var client = new CouchClient ( "http://localhost" ) )
129132 {
130- httpTest . RespondWith ( "Not found" , 404 ) ;
133+ httpTest . RespondWith ( "Not found" , 404 ) ;
131134 var result = await client . IsUpAsync ( ) ;
132135 Assert . False ( result ) ;
133136 }
@@ -175,5 +178,83 @@ public async Task ActiveTasks()
175178 }
176179
177180 #endregion
181+
182+ #region Error Handling
183+ [ Fact ]
184+ public async Task ConflictException ( )
185+ {
186+ using ( var httpTest = new HttpTest ( ) )
187+ {
188+ httpTest . RespondWith ( status : ( int ) HttpStatusCode . Conflict ) ;
189+
190+ using ( var client = new CouchClient ( "http://localhost" ) )
191+ {
192+ await Assert . ThrowsAsync < CouchConflictException > ( ( ) => client . CreateDatabaseAsync < Rebel > ( ) ) ;
193+ }
194+ }
195+ }
196+
197+ [ Fact ]
198+ public async Task NotFoundException ( )
199+ {
200+ using ( var httpTest = new HttpTest ( ) )
201+ {
202+ httpTest . RespondWith ( status : ( int ) HttpStatusCode . NotFound ) ;
203+
204+ using ( var client = new CouchClient ( "http://localhost" ) )
205+ {
206+ await Assert . ThrowsAsync < CouchNotFoundException > ( ( ) => client . DeleteDatabaseAsync < Rebel > ( ) ) ;
207+ }
208+ }
209+ }
210+
211+ [ Fact ]
212+ public void BadRequestException ( )
213+ {
214+ using ( var httpTest = new HttpTest ( ) )
215+ {
216+ httpTest . RespondWith ( @"{error: ""no_usable_index""}" , ( int ) HttpStatusCode . BadRequest ) ;
217+
218+ using ( var client = new CouchClient ( "http://localhost" ) )
219+ {
220+ var db = client . GetDatabase < Rebel > ( ) ;
221+ Assert . Throws < CouchNoIndexException > ( ( ) => db . UseIndex ( "aoeu" ) . ToList ( ) ) ;
222+ }
223+ }
224+ }
225+
226+ [ Fact ]
227+ public async Task GenericExceptionWithMessage ( )
228+ {
229+ using ( var httpTest = new HttpTest ( ) )
230+ {
231+ string message = "message text" ;
232+ string reason = "reason text" ;
233+ httpTest . RespondWith ( $ "{{error: \" { message } \" , reason: \" { reason } \" }}", ( int ) HttpStatusCode . InternalServerError ) ;
234+
235+ using ( var client = new CouchClient ( "http://localhost" ) )
236+ {
237+ var db = client . GetDatabase < Rebel > ( ) ;
238+ var couchException = await Assert . ThrowsAsync < CouchException > ( ( ) => db . FindAsync ( "aoeu" ) ) ;
239+ Assert . Equal ( message , couchException . Message ) ;
240+ }
241+ }
242+ }
243+
244+ [ Fact ]
245+ public async Task GenericExceptionNoMessage ( )
246+ {
247+ using ( var httpTest = new HttpTest ( ) )
248+ {
249+ httpTest . RespondWith ( status : ( int ) HttpStatusCode . InternalServerError ) ;
250+
251+ using ( var client = new CouchClient ( "http://localhost" ) )
252+ {
253+ var db = client . GetDatabase < Rebel > ( ) ;
254+ var couchException = await Assert . ThrowsAsync < CouchException > ( ( ) => db . FindAsync ( "aoeu" ) ) ;
255+ }
256+ }
257+ }
258+ #endregion
178259 }
179260}
0 commit comments