Table of Contents

Mercredi - Interfaçages dans un contexte d'administration système et réseau

Navigation rapide : Lundi / Mardi / Mercredi / Jeudi / Vendredi Mémos : Perl / Python / Ruby

17h00 - Fil rouge « Client WEB »

Finalité

Nous allons voir ici :

Corrigé

1

Indication : utiliser la bibliothèque standard 'socket', objet 'TCPSocket'

réponse_1

 1: %w(socket pp).each do |lib| require lib end
 2:
 3: begin
 4:   client = TCPSocket.new('listes.example.com','www')
 5: rescue Exception => e
 6:   pp e
 7: else
 8:   client.print [ "GET /anfexo/plm.html HTTP/1.1", "Host: listes.example.com" ].join("\n")+"\n\n"
 9:   puts client.gets(nil)
10: ensure
11:   client.close
12: end

2a

Indication : utiliser la bibliothèque standard 'open-uri'.

réponse_2a

1: require 'open-uri'
2:
3: puts open('http://listes.example.com/anfexo/plm.html').read

2b

Indication : utiliser la gem 'patron' qui s'appuie sur la bibliothèque C 'libcurl'

réponse_2b

1: %w(rubygems patron pp).each do |lib| require lib end
2:
3: session          = Patron::Session.new
4: session.base_url = 'http://listes.example.com'
5: response         = session.get('/anfexo/plm.html')
6: pp response
7: pp response.headers
8: pp response.body

3

Indication : utiliser la bibliothèque standard 'test/unit'

réponse_3

 1: %w(rubygems patron ap nokogiri).each do |lib| require lib end
 2:
 3: class Form
 4:   attr_reader :action, :input
 5:   def initialize
 6:     @session          = Patron::Session.new
 7:     @session.base_url = 'http://listes.example.com'
 8:     response          = @session.get('/cgi-bin/form.cgi')
 9:     doc               = Nokogiri::HTML(response.body)
10:     form              = doc.xpath('//form')
11:     @action           = form.attribute('action').value
12:     inputs            = doc.xpath('//form/input')
13:     @input = []
14:     inputs.each do |input| @input << input.attribute('name').value end
15:   end
16:   def fill(nom,prenom,email)
17:     @fields = { 'nom' => nom, 'prenom' => prenom, 'email' => email }
18:   end
19:   def submit
20:     fill('leonardo','davinci','leonardo.davinci@closluce.com')
21:     response = @session.post(@action,@fields)
22:     return response.status
23:   end
24: end
25:
26: @@form = Form.new

my_unit_test.rb

 1: require 'test/unit'
 2:
 3: class TestForm < Test::Unit::TestCase
 4:   def test_have_three_fields_named_nom_prenom_email
 5:     must = [ 'nom','prenom','email' ]
 6:     result = {
 7:       :'form.input' => @@form.input,
 8:       :'must' => must,
 9:       :'form.input&must' => @@form.input&must,
10:       :'form.input&must==must'=> (@@form.input&must==must)
11:     }
12:     ap result
13:     assert(@@form.input&must==must)
14:   end
15:   def test_submission
16:     assert_equal(@@form.submit,200)
17:   end
18: end

console

Loaded suite etape3
Started
{
   :"form.input" => [
        [0] "nom",
        [1] "prenom",
        [2] "email",
        [3] "Envoyer"
   ],
   :must => [
        [0] "nom",
        [1] "prenom",
        [2] "email"
   ],
   :"form.input&must" => [
        [0] "nom",
        [1] "prenom",
        [2] "email"
   ],
   :"form.input&must==must" => true
}
..
Finished in 0.04368 seconds.

2 tests, 2 assertions, 0 failures, 0 errors

Version

$Id: mercredi_corrige_ruby_2.txt 659 2012-05-23 15:37:51Z delavennat $