|
| 1 | +unit TestRestClient; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +{$I DelphiRest.inc} |
| 6 | + |
| 7 | +uses TestFramework, RestClient, HttpConnection, Classes, SysUtils; |
| 8 | + |
| 9 | +type |
| 10 | + TTestRestClient = class(TTestCase) |
| 11 | + private |
| 12 | + FRest: TRestClient; |
| 13 | + FCustomCreateConnectionCalled: Boolean; |
| 14 | + |
| 15 | + procedure OnCreateCustomConnectionNull(Sender: TObject; AConnectionType: THttpConnectionType; out AConnection: IHttpConnection); |
| 16 | + procedure OnCreateCustomConnection(Sender: TObject; AConnectionType: THttpConnectionType; out AConnection: IHttpConnection); |
| 17 | + protected |
| 18 | + procedure TearDown; override; |
| 19 | + procedure SetUp; override; |
| 20 | + published |
| 21 | + procedure InvalidCustomConnectionConfiguration; |
| 22 | + procedure InvalidCustomConnectionConfigurationResult; |
| 23 | + procedure UsingCustomConnection; |
| 24 | + procedure RaiseExceptionWhenInactiveConnection; |
| 25 | + procedure RaiseExceptionWhenInactiveConnection2; |
| 26 | + end; |
| 27 | + |
| 28 | +implementation |
| 29 | + |
| 30 | +type |
| 31 | + IStubConnection = interface(IHttpConnection) |
| 32 | + ['{9CCD27F6-ADA6-47CE-84C4-B7126F8D3281}'] |
| 33 | + end; |
| 34 | + |
| 35 | + TStubConnection = class(TInterfacedObject, IHttpConnection, IStubConnection) |
| 36 | + function SetAcceptTypes(AAcceptTypes: string): IHttpConnection;virtual;abstract; |
| 37 | + function SetContentTypes(AContentTypes: string): IHttpConnection;virtual;abstract; |
| 38 | + function SetAcceptedLanguages(AAcceptedLanguages: string): IHttpConnection;virtual;abstract; |
| 39 | + function SetHeaders(AHeaders: TStrings): IHttpConnection;virtual;abstract; |
| 40 | + |
| 41 | + procedure Get(AUrl: string; AResponse: TStream);virtual;abstract; |
| 42 | + procedure Post(AUrl: string; AContent, AResponse: TStream);virtual;abstract; |
| 43 | + procedure Put(AUrl: string; AContent, AResponse: TStream);virtual;abstract; |
| 44 | + procedure Delete(AUrl: string; AContent: TStream);virtual;abstract; |
| 45 | + |
| 46 | + function GetResponseCode: Integer;virtual;abstract; |
| 47 | + function GetEnabledCompression: Boolean;virtual;abstract; |
| 48 | + |
| 49 | + procedure SetEnabledCompression(const Value: Boolean);virtual;abstract; |
| 50 | + end; |
| 51 | + |
| 52 | +{ TTestRestClient } |
| 53 | + |
| 54 | +procedure TTestRestClient.InvalidCustomConnectionConfiguration; |
| 55 | +begin |
| 56 | + ExpectedException := EInvalidHttpConnectionConfiguration; |
| 57 | + FRest.ConnectionType := hctCustom; |
| 58 | +end; |
| 59 | + |
| 60 | +procedure TTestRestClient.InvalidCustomConnectionConfigurationResult; |
| 61 | +begin |
| 62 | + ExpectedException := ECustomCreateConnectionException; |
| 63 | + FRest.OnCustomCreateConnection := OnCreateCustomConnectionNull; |
| 64 | + FRest.ConnectionType := hctCustom; |
| 65 | +end; |
| 66 | + |
| 67 | +procedure TTestRestClient.OnCreateCustomConnection(Sender: TObject; AConnectionType: THttpConnectionType; out AConnection: IHttpConnection); |
| 68 | +begin |
| 69 | + FCustomCreateConnectionCalled := True; |
| 70 | + {$WARN CONSTRUCTING_ABSTRACT OFF} |
| 71 | + AConnection := TStubConnection.Create; |
| 72 | + {$WARN CONSTRUCTING_ABSTRACT ON} |
| 73 | +end; |
| 74 | + |
| 75 | +procedure TTestRestClient.OnCreateCustomConnectionNull(Sender: TObject; |
| 76 | + AConnectionType: THttpConnectionType; out AConnection: IHttpConnection); |
| 77 | +begin |
| 78 | + AConnection := nil; |
| 79 | +end; |
| 80 | + |
| 81 | +procedure TTestRestClient.RaiseExceptionWhenInactiveConnection; |
| 82 | +begin |
| 83 | + ExpectedException := EInactiveConnection; |
| 84 | + FRest.ResponseCode; |
| 85 | +end; |
| 86 | + |
| 87 | +procedure TTestRestClient.RaiseExceptionWhenInactiveConnection2; |
| 88 | +begin |
| 89 | + ExpectedException := EInactiveConnection; |
| 90 | + FRest.Resource('https://www.google.com.br').Get; |
| 91 | +end; |
| 92 | + |
| 93 | +procedure TTestRestClient.SetUp; |
| 94 | +begin |
| 95 | + inherited; |
| 96 | + FRest := TRestClient.Create(nil); |
| 97 | + FRest.Name := ClassName + '_' + FRest.ClassName;; |
| 98 | +end; |
| 99 | + |
| 100 | +procedure TTestRestClient.TearDown; |
| 101 | +begin |
| 102 | + inherited; |
| 103 | + FRest.Free; |
| 104 | +end; |
| 105 | + |
| 106 | +procedure TTestRestClient.UsingCustomConnection; |
| 107 | +begin |
| 108 | + FRest.OnCustomCreateConnection := OnCreateCustomConnection; |
| 109 | + FRest.ConnectionType := hctCustom; |
| 110 | + |
| 111 | + CheckTrue(FCustomCreateConnectionCalled); |
| 112 | + CheckTrue(Supports(FRest.UnWrapConnection, IStubConnection)); |
| 113 | +end; |
| 114 | + |
| 115 | +initialization |
| 116 | + RegisterTest(TTestRestClient.Suite); |
| 117 | + |
| 118 | +end. |
0 commit comments