[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[nodeJS] 300 Request

"300-1 Simple request with #request:with:"
| options content client |
options := (
	(#host -> 'u8.smalltalking.net'), 
	(#path ->'/profile/smalltalking/U8/License.txt'),
        (#agent -> false)
).	
content := ''.	
client := NodeJS http 
	request: options
	with: [:res| | response |
		response := NodeIncomingMessage @ res.
		response on: #data do: [:data|
			content := content ,data
		].
		response on: #end do:["self print: 'Content size =',content size"]
	].								
client on: #error do: [:ex| ^self error: '300-1 ' ,ex#message ].
client end.	
client isNil ifTrue: [ ^self error: '300-1 Client is nil' ].
self print: 'OK->300-1 Simple request with #request:with:'.


"300-2 Simple request with #get:with:"
| content client |
content := ''.
client := NodeJS http 
	   get: 'http://u8.smalltalking.net/profile/smalltalking/U8/License.txt'
	   with: [:res| | response |
	  	   response := NodeIncomingMessage @ res.
		   response on: #data do:[:data| content := content,data ].
		   response on: #end do:[ "self print: 'content size=',content size" ].
		   response resume ].		
client on: #error do: [:ex| ^self error: '300-2 ' ,ex#message ].
self print: 'OK->300-2 Simple request with #get:with:'.


"300-3 Simple request with #get:. Only for testing purpose."
| client |
client := NodeJS http get: 'http://u8.smalltalking.net/profile/smalltalking/U8/License.txt'.	
client on: #error do: [:ex| ^self error: '300-3 ' ,ex#message ].
self print: 'OK->300-3 Simple request with #get:'.


"300-4 Posting data with #end:"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'POST') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use #data and #end events to process response"].
client on: #error do: [:ex| ^self error:'003-4 ', ex#message ].
client end: 'posting test data'.
self print: 'OK->300-4 Posting data with #end:'.


"300-5 Posting data with #end:encoding:"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'POST') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to process response"].
client on: #error do: [:ex|
    ^self error:'300-5 ', ex#message
].
client end: 'posting test data' encoding: #utf8.
self print: 'OK->300-5 Posting data with #end:encoding:'.


"300-6 Posting data with #end:encoding:with:"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'POST') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-6 ', ex#message ].
client end: 'posting test data' encoding: #utf8 with:["do something on posting end"].
self print: 'OK->300-6 Posting data with #end:encoding:with:'.


"300-7 Posting data with #write: #end"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'POST') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'003-7 ', ex#message ].
client write: 'posting test data'.
client end.
self print: 'OK->300-7 Posting data with #write: #end'.


"300-8 Posting data with #write:encoding: #end"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'POST') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-8 ', ex#message ].
client write: 'posting test data' encoding: #utf8.
client end.
self print: 'OK->300-8 Posting data with #write:encoding: #end'.


"300-9 Posting data with #write:encoding:with: #end"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'POST') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-9 ', ex#message ].
client write: 'posting test data' encoding: #utf8 with:[self print: 'flushed data'].
client end.
self print: 'OK->300-9 Posting data with #write:encoding:with: #end'.


"300-10 Aborting a request"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
	    (#path -> '/'),
            (#agent -> false),
	    (#method -> 'GET') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| "Aborting a request will throw an error" ].
client abort.
client end.
self print: 'OK->300-10 Aborting a request'.


"300-11 Doing request with #setNoDelay"
| options client |
options := ((#host -> 'u8.smalltalking.net'),
            (#agent -> false), 
	    (#path -> '/') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-11 ', ex#stack].
client setNoDelay.
client end.
self print: 'OK->300-11 Doing request with #setNoDelay'.


"300-12 Doing request with #setNoDelay:"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
            (#agent -> false), 
	    (#path -> '/') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-12 ', ex#message ].
client setNoDelay: true.
client end.
self print: 'OK->300-12 Doing request with #setNoDelay:'.


"300-13 Doing request with #setSocketKeepAlive:"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
            (#path -> '/') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'003-13 ', ex#message ].
client setSocketKeepAlive: false.
client end.
self print: 'OK->300-13 Doing request with #setSocketKeepAlive:'.


"300-14 Doing request with #setSocketKeepAlive:initialDelay:"
| options client |
options := ((#host -> 'u8.smalltalking.net'), 
            (#path -> '/') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-14', ex#message ].
client setSocketKeepAlive: false initialDelay: 0.
client end.
self print: 'OK->300-14 Doing request with #setSocketKeepAlive:initialDelay:'.


"300-15 Doing request with #setTimeout:"
| options client |
options := ((#host -> 'u8.smalltalking.net'),
            (#agent -> false), 
	    (#path -> '/') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [:ex| ^self error:'300-15 ', ex#message ].
client setTimeout:10.
client end.
self print: 'OK->300-15 Doing request with #setTimeout:'.


"300-16 Doing request with #setTimeout:with:"
| options client |
options := ((#host -> 'u8.smalltalking.net'),
            (#agent -> false),  
	    (#path -> '/') ).	
client := NodeJS http 
	    request: options
	    with: [:response| "use events #data and #end to proccess response"].
client on: #error do: [ ^self error:'003-16 NodeHTTPRequestClient>>setTimeout:with: ' ].
client setTimeout:1000 with: [ self print: 'OK 300-16 Time out reached'].
client end.
self print: 'OK->300-16 Doing request with #setTimeout:with:'.