Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ZnProxyServerDelegate[Test] #147

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
I am a ZnDelegate that acts as a proxy.

I handle requests by passing them to a proxyBlock which can change the request before I execute it.
The idea is that the changed request is to a different server, the one that we proxy.

Usage example

| proxiedServer proxyServer proxyDelegate client |
proxiedServer := ZnServer startOn: 8080.
proxyServer := ZnServer on: 9090.
proxyDelegate := ZnProxyServerDelegate new
server: proxyServer;
proxyBlock: [ :request |
request url: (request url port: 8080) ].
proxyServer
delegate: proxyDelegate;
start.
client := ZnClient new.
client get: proxyServer url / #echo.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
private
augmentUrl: aUrl
"The URL in a server request has no scheme, host or port set.
Augment aUrl by taking those elements from #serverUrl"

self server ifNil: [ ^ aUrl ].
^ aUrl inContextOf: self server url
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
private
copyAndPrepareRequest: aRequest
"We make a copy of the request and augment its URL"

| copiedRequest |
copiedRequest := aRequest copy.
copiedRequest url: (self augmentUrl: copiedRequest url).
^ copiedRequest
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public
handleRequest: incomingRequest
"Ask our proxy block to transform a incoming ZnRequest copy into an outgoing ZnRequest"

| outgoingRequest |
outgoingRequest := self proxyBlock
cull: (self copyAndPrepareRequest: incomingRequest)
cull: self server.
^ ZnClient new
request: outgoingRequest;
beOneShot;
execute;
response
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
accessing
proxyBlock: aBlock
"Set the block that transforms a copy of the incoming ZnRequest to the outgoing ZnRequest.
Two parameters are passed to block: the request and an optional server reference.
See #copyAndPrepareRequest: for how the request got changed."

proxyBlock := aBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
proxyBlock
^ proxyBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
accessing
server: aZnServer
"Set areference to the server we're in (optional)"

server := aZnServer
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
server
^ server
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"commentStamp" : "<historical>",
"super" : "Object",
"category" : "Zinc-HTTP-Examples",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"proxyBlock",
"server"
],
"name" : "ZnProxyServerDelegate",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
testing
testEcho
| proxiedServer proxyServer proxyDelegate client |
proxiedServer := ZnServer startOn: 8080.
proxyServer := ZnServer on: 9090.
proxyDelegate := ZnProxyServerDelegate new
server: proxyServer;
proxyBlock: [ :request |
request url: (request url port: 8080) ].
proxyServer
delegate: proxyDelegate;
start.
client := ZnClient new.
client get: proxyServer url / #echo.
self assert: client isSuccess.
self assert: (client contents includesSubstring: 'echoing').
self assert: (client contents includesSubstring: 'running 8080').
proxiedServer stop.
proxyServer stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"commentStamp" : "",
"super" : "TestCase",
"category" : "Zinc-HTTP-Examples",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "ZnProxyServerDelegateTest",
"type" : "normal"
}
Loading