[fpc-pascal] Using fphttpclient to simulate web action sequence

Michael Van Canneyt michael at freepascal.org
Wed Apr 29 13:43:02 CEST 2015



On Wed, 29 Apr 2015, leledumbo wrote:

> I need to create some kind of bot for automatic testing against our web app.
> The following app target real online web application:
>
> {$mode objfpc}{$H+}
>
> uses
>  {$ifdef unix}
>  cthreads,
>  {$endif}
>  Classes,SysUtils,fphttpclient;
>
> procedure Log(http: TFPHTTPClient);
> begin
>  WriteLn('BEGIN
> **********************************************************');
>  WriteLn(http.RequestHeaders.Text);
>  WriteLn(http.ResponseStatusCode);
>  WriteLn(http.ResponseHeaders.Text);
>  WriteLn('END **********************************************************');
> end;
>
> var
>  http: TFPHTTPClient;
>  formdata: TStrings;
> begin
>  http := TFPHTTPClient.Create(nil);
>  try
>    // initial GET request to initialize cookies
>    http.Get('http://lelelagi.mysirclo.com');
>    Log(http);
>
>    // POST: add item to cart
>    formdata := TStringList.Create;
>    formdata.Values['cmd'] := 'add';
>    formdata.Values['item_id'] := 'montauk-tote';
>    formdata.Values['member_email'] := 'leledumbo_cool at yahoo.co.id ';
>    http.FormPost('http://lelelagi.mysirclo.com/cart',formdata); // #1
>    Log(http);
>
>    // POST: set country, city and shipping
>    formdata.Clear;
>    formdata.Values['cmd'] := 'shipping_country';
>    formdata.Values['shipping_value'] := 'ID';
>    http.FormPost('http://lelelagi.mysirclo.com/cart',formdata); // #2
>    Log(http);
>
>    formdata.Clear;
>    formdata.Values['cmd'] := 'shipping_city';
>    formdata.Values['shipping_value'] := 'Kota Depok - Depok';
>    http.FormPost('http://lelelagi.mysirclo.com/cart',formdata);
>    Log(http);
>
>    formdata.Clear;
>    formdata.Values['cmd'] := 'shipping';
>    formdata.Values['shipping_value'] := 'JNE REG';
>    http.FormPost('http://lelelagi.mysirclo.com/cart',formdata);
>    Log(http);
>
>    // POST: place order
>    formdata.Clear;
>    formdata.Values['first_name'] := 'Test Name';
>    formdata.Values['phone'] := '088712342345';
>    formdata.Values['address_line1'] := 'Certain Street';
>    formdata.Values['country'] := 'ID';
>    formdata.Values['state'] := 'DKI Jakarta';
>    formdata.Values['city'] := 'DKI Jakarta - Jakarta';
>    formdata.Values['postal_code'] := '12424';
>    formdata.Values['email'] := 'mail at testmail.co.id';
>    formdata.Values['delivery_first_name'] := 'Test Name';
>    formdata.Values['delivery_phone'] := '088712342345';
>    formdata.Values['delivery_address_line1'] := 'Certain Street';
>    formdata.Values['delivery_country'] := 'ID';
>    formdata.Values['delivery_state'] := 'DKI Jakarta';
>    formdata.Values['delivery_city'] := 'DKI Jakarta - Jakarta';
>    formdata.Values['delivery_postal_code'] := '12424';
>    formdata.Values['delivery_email'] := 'mail at testmail.co.id';
>    formdata.Values['shipping_value'] := 'JNE REG';
>    formdata.Values['payment_method'] := 'bank-transfer';
>    formdata.Values['message'] := '';
>    formdata.Values['agreement'] := 'on';
>
> http.FormPost('http://lelelagi.mysirclo.com/cart/place_order/guest',formdata);
>    Log(http);
>  finally
>    http.Free;
>  end;
> end.
>
> The app always dies with "EHTTPClient: Error reading data from socket" after
> the first FormPost, any of them (e.g. if you comment #1, it will die after
> #2). Did I miss something? Is this the correct way to make such a
> simulation?

Yes it is. 
If the problem disappears after you do a Free and Create of the http class, 
then it means something is left hanging after the call. 
In that case I would need to check what is happening.

If the problem persists, then it means there is some mechanism on the server that
is causing this.

Michael.



More information about the fpc-pascal mailing list