-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGRestJWTClient.dpr
More file actions
115 lines (109 loc) · 4.03 KB
/
GRestJWTClient.dpr
File metadata and controls
115 lines (109 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
program GRestJWTClient;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes,
System.JSON,
System.Net.HttpClient,
System.Net.URLClient,
System.NetEncoding;
var
HttpClient: THTTPClient;
Response: IHTTPResponse;
LoginJson, ResponseJson: TJSONObject;
Token: string;
begin
try
HttpClient := THTTPClient.Create;
try
HttpClient.ConnectionTimeout := 3000;
HttpClient.ResponseTimeout := 3000;
WriteLn('=== Test GRestSvrJWTPrj ===');
WriteLn;
WriteLn('1. Getting JWT token...');
LoginJson := TJSONObject.Create;
try
LoginJson.AddPair('username', 'admin');
LoginJson.AddPair('password', 'admin');
try
Response := HttpClient.Post(
'http://localhost:3042/api/token',
TStringStream.Create(LoginJson.ToJSON),
nil,
TNetHeaders.Create(TNameValuePair.Create('Content-Type', 'application/json'))
);
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
if Response.StatusCode = 200 then
begin
ResponseJson := TJSONObject.ParseJSONValue(Response.ContentAsString) as TJSONObject;
try
if Assigned(ResponseJson) then
begin
Token := ResponseJson.GetValue<string>('token', '');
if Token <> '' then
begin
WriteLn('JWT Token obtained successfully!');
WriteLn('2. Calling protected endpoint with JWT token...');
try
Response := HttpClient.Get(
'http://localhost:3042/api/autotest',
nil,
TNetHeaders.Create(TNameValuePair.Create('Authorization', 'Bearer ' + Token))
);
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
WriteLn('3. Calling protected endpoint with invalid token...');
Response := HttpClient.Get(
'http://localhost:3042/api/autotest',
nil,
TNetHeaders.Create(TNameValuePair.Create('Authorization', 'Bearer invalid.token.value'))
);
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
WriteLn('4. Calling protected endpoint without token...');
Response := HttpClient.Get('http://localhost:3042/api/autotest');
WriteLn('Status: ', Response.StatusCode, ' ', Response.StatusText);
WriteLn('Response: ', Response.ContentAsString);
WriteLn;
except
on E: Exception do
WriteLn('Error calling protected endpoint: ', E.Message);
end;
end
else
WriteLn('Error: Token not found in response.');
end
else
WriteLn('Error: Invalid JSON response.');
finally
ResponseJson.Free;
end;
end
else
WriteLn('Error: Could not obtain token. Check if server is running.');
except
on E: Exception do
WriteLn('Error obtaining token: ', E.Message);
end;
finally
LoginJson.Free;
end;
finally
HttpClient.Free;
end;
WriteLn;
WriteLn('=== End of test ===');
WriteLn('Press Enter to exit...');
ReadLn;
except
on E: Exception do
begin
WriteLn('Unexpected error: ', E.Message);
ReadLn;
end;
end;
end.