1
+ using ksqlDB . RestApi . Client . KSql . Query . Context ;
2
+ using ksqlDB . RestApi . Client . KSql . RestApi . Http ;
3
+ using ksqlDb . RestApi . Client . ProtoBuf . KSql . RestApi ;
4
+ using Moq . Protected ;
5
+ using Moq ;
6
+ using System . Net ;
7
+
8
+ namespace ksqlDb . RestApi . Client . ProtoBuf . Tests . KSql . RestApi ;
9
+
10
+ internal class TestableKSqlDbQueryProvider : KSqlDbQueryProvider
11
+ {
12
+ internal const string KsqlDbUrl = @"http:\\localhost:8088" ;
13
+
14
+ public static readonly KSqlDBContextOptions KSqlDbContextOptionsInstance = new ( KsqlDbUrl )
15
+ {
16
+ JsonSerializerOptions =
17
+ ksqlDb . RestApi . Client . KSql . Query . Context . Options . KSqlDbJsonSerializerOptions . CreateInstance ( )
18
+ } ;
19
+
20
+ public TestableKSqlDbQueryProvider ( IHttpV1ClientFactory httpClientFactory )
21
+ : base ( httpClientFactory , KSqlDbContextOptionsInstance )
22
+ {
23
+ }
24
+
25
+ protected string QueryResponse = "[{\" header\" :{\" queryId\" :\" _confluent-ksql-default_transient_9174388154324047204_1614627435343\" ,\" schema\" :\" `ID` INTEGER, `ARR` ARRAY<STRUCT<`TITLE` STRING, `ID` INTEGER>>, `MAPVALUE` MAP<STRING, MAP<STRING, INTEGER>>, `MAPARR` MAP<INTEGER, ARRAY<STRING>>, `STR` STRUCT<`TITLE` STRING, `ID` INTEGER>, `RELEASE_YEAR` INTEGER\" }},\r \n {\" row\" :{\" columns\" :[1,[{\" TITLE\" :\" Aliens\" ,\" ID\" :1},{\" TITLE\" :\" test\" ,\" ID\" :2}],{\" a\" :{\" a\" :1,\" b\" :2},\" b\" :{\" d\" :4,\" c\" :3}},{\" 1\" :[\" a\" ,\" b\" ],\" 2\" :[\" c\" ,\" d\" ]},{\" TITLE\" :\" Aliens\" ,\" ID\" :1},1986]}},\r \n {\" row\" :{\" columns\" :[2,[{\" TITLE\" :\" Die Hard\" ,\" ID\" :2},{\" TITLE\" :\" test\" ,\" ID\" :2}],{\" a\" :{\" a\" :1,\" b\" :2},\" b\" :{\" d\" :4,\" c\" :3}},{\" 1\" :[\" a\" ,\" b\" ],\" 2\" :[\" c\" ,\" d\" ]},{\" TITLE\" :\" Die Hard\" ,\" ID\" :2},1998]}}," ;
26
+
27
+ protected override HttpClient OnCreateHttpClient ( )
28
+ {
29
+ return FakeHttpClient . CreateWithResponse ( QueryResponse ) ; ;
30
+ }
31
+ }
32
+ public static class FakeHttpClient
33
+ {
34
+ public static Mock < DelegatingHandler > CreateDelegatingHandler ( string responseContent , HttpStatusCode statusCode = HttpStatusCode . OK )
35
+ {
36
+ var handlerMock = new Mock < DelegatingHandler > ( ) ;
37
+
38
+ handlerMock
39
+ . Protected ( )
40
+ . Setup < Task < HttpResponseMessage > > (
41
+ nameof ( HttpClient . SendAsync ) ,
42
+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
43
+ ItExpr . IsAny < CancellationToken > ( )
44
+ )
45
+ . ReturnsAsync ( new HttpResponseMessage
46
+ {
47
+ StatusCode = statusCode ,
48
+ Content = new StringContent ( responseContent ) ,
49
+ } )
50
+ . Verifiable ( ) ;
51
+
52
+ return handlerMock ;
53
+ }
54
+
55
+ public static Mock < HttpMessageHandler > CreateHttpMessageHandler ( string responseContent , HttpStatusCode statusCode = HttpStatusCode . OK )
56
+ {
57
+ var handlerMock = new Mock < HttpMessageHandler > ( ) ;
58
+
59
+ handlerMock
60
+ . Protected ( )
61
+ . Setup < Task < HttpResponseMessage > > (
62
+ nameof ( HttpClient . SendAsync ) ,
63
+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
64
+ ItExpr . IsAny < CancellationToken > ( )
65
+ )
66
+ . ReturnsAsync ( new HttpResponseMessage
67
+ {
68
+ StatusCode = statusCode ,
69
+ Content = new StringContent ( responseContent ) ,
70
+ } )
71
+ . Verifiable ( ) ;
72
+
73
+ return handlerMock ;
74
+ }
75
+
76
+ public static HttpClient ToHttpClient ( this Mock < HttpMessageHandler > handlerMock )
77
+ {
78
+ return new HttpClient ( handlerMock . Object )
79
+ {
80
+ BaseAddress = new Uri ( TestableKSqlDbQueryProvider . KsqlDbUrl )
81
+ } ;
82
+ }
83
+
84
+ public static HttpClient CreateWithResponse ( string responseContent , HttpStatusCode statusCode = HttpStatusCode . OK )
85
+ {
86
+ var handlerMock = CreateHttpMessageHandler ( responseContent , statusCode ) ;
87
+
88
+ return handlerMock . ToHttpClient ( ) ;
89
+ }
90
+ }
0 commit comments