Time: 3 January 2017, 2:02:24.562318 pm
add test for chunk stream reading
=============== Diff against WebClient-Tests-topa.48 ===============
Item was added:
+ ----- Method: WebClientServerTest>>testChunkedLoopbackWithStream (in category 'tests - chunked') -----
+ "Test HTTP loopback streaming using chunked transfer-encoding"
+ | queue response stream |
+ queue := SharedQueue new.
+ server addService: '/recv' action:[:req |
+ "The /recv service establishes the write-end for the server.
+ In a real environment we would access protect the request
+ and also pass a token to be used to link the incoming /send
+ request from the client."
+ req sendResponse: 200 chunked:[:writeEnd|
+ | chunk |
+ "There is no reason to wait for the client to send a request,
+ the protocol is entirely freestyle. Send something just because
+ we can"
+ writeEnd nextChunkPut: 'Initial response'.
+ "And from here on echo any incoming data"
+ [chunk := queue next.
+ chunk == nil] whileFalse:[writeEnd nextChunkPut: chunk].
+ "And some final data"
+ writeEnd nextChunkPut: 'Final response'.
+ ] methods: #('GET'). "only allow GET requests"
+ server addService: '/send' action:[:req |
+ "The /send service establishes the read-end for the server.
+ Simply read the chunks as they come in and stick them in
+ our loopback queue to send them back to the client."
+ | chunk |
+ [chunk := req nextChunk.
+ chunk == nil] whileFalse:[queue nextPut: chunk].
+ queue nextPut: nil. "end conversation"
+ req send200Response: 'ok'.
+ ] methods: #('POST'). "only allow POST requests"
+ "Establish the server response stream"
+ response := WebClient new httpGet: self localHostUrl, '/recv'.
+ self assert: response code = 200.
+ self assert: (response headerAt: 'Transfer-Encoding') = 'chunked'.
+ stream := response contentStream.
+ self assert: (stream next: 'Initial response' size) equals: 'Initial response'.
+ "Establish the client request stream"
+ httpPostChunked: self localHostUrl, '/send'
+ "We've set up both ends, try our loopback server"
+ request nextChunkPut: 'Hello World'.
+ self assert: (stream next: 'Hello World' size) equals: 'Hello World'.
+ request nextChunkPut: 'The answer is 42'.
+ self assert: (stream next: 'The answer is 42' size) equals: 'The answer is 42'.
+ ] type: nil.
+ self assert: (stream next: 'Final response' size) equals: 'Final response'.