The document discusses testing HTTP requests and responses in Ruby. It provides examples of using tools like Wireshark and HTTP Analyzer to capture real HTTP traffic. It then shows how to replay the same requests and validate the responses programmatically in Ruby using libraries like Net::HTTP, Mechanize and FakeWeb. The goal is to test HTTP interactions without relying on the actual backend services.
Handwritten Text Recognition for manuscripts and early printed texts
RUBY API DEVELOPMENT
1. Ruby application based on http www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
2.
3. www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
4. Http Server www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Oauth JSON XML Params Signature Web Service Restful Cloud Computing HTML XHTML
5.
6. Ruby API? Official? Community? www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
7. Get data from third-party website www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development if the website provides an api if the website provides an official/community ruby api use the ruby api else write the ruby api by your self end else write the ruby codes act as a http client (browser) end
8. What if fetch contacts from gmail? What if fetch timelines from twitter? What if fetch friends from facebook? What if fetch contacts from msn? What if fetch repositories from github? Lucky, there are ruby api to do theses! www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
9. What if fetch contacts from 163 mail? What if fetch weather from sina? What if fetch friends from kaixin001? What if fetch contacts from fetion? T here are no ruby api to do theses! www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
10.
11. Sniffer www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
12.
13. Wireshark www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development filter http one http request TCP/IP HTTP HTTP Header/Body Raw Data and Printable text
14. Wireshark www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development one http response
15.
16. HTTP Analyzer www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development http request header http response header
17. HTTP Analyzer www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development http response body
18. HTTP Analyzer www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development http request timing
19. Get session/new www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Get /session/new HTTP/1.1 Host: rubyconfchina.org Connect: keep-alive User-Agent: Mozilla/5.0 AppleWebKit/553.4 Accept: text/html,application/xhtml+xml,text/html;q=0.9 Accept-Encoding: gzip,deflate Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
20. Get session/new response www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development HTTP/1.1 200 OK Server: nginx/0.5.32 Date: Sun, 20 Jun 2010 09:25:08 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Cache-Control: private, max-age=0, must-revalidate Content-Encoding: gzip <!DOCTYPE html PUBLIC …> <html> …
21. Post /session www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Post /session HTTP/1.1 Host: rubyconfchina.org Connect: keep-alive User-Agent: Mozilla/5.0 AppleWebKit/553.4 Referer: http://rubyconfchina.org/session/new Content-Length: 106 Content-Type: application/x-www-form-urlencoded Accept: text/html,application/xhtml+xml,text/html;q=0.9 authenticity_token=…&login=flyerhzm&password=… &commit=Log+in
22. Post /session response www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development HTTP/1.1 302 Moved Temporarily Server: nginx/0.5.32 Date: Sun, 20 Jun 2010 09:25:31 GMT Content-Type: text/html; charset=utf-8 Connect: keep-alive Location: http://rubyconfchina.org/ Cache-Control: no-cache Set-Cookie: auth_token=; _euruko_session=BAh7CTo Content-Length: 91 <html><body>You are being <a href=http://rubyconfchina.org/>redirected</a>. </body></html>
23. Get / www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Get / HTTP/1.1 Host: rubyconfchina.org Connect: keep-alive User-Agent: Mozilla/5.0 AppleWebKit/553.4 Accept: text/html,application/xhtml+xml,text/html;q=0.9 Accept-Encoding: gzip,deflate Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Cookie: auth_token=; _euruko_session=BAh7CTo
24. Http protocol www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development HTTP/1.1 200 OK Server: nginx/0.5.32 Date: Sun, 20 Jun 2010 09:25:08 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Cache-Control: private, max-age=0, must-revalidate Content-Encoding: gzip <!DOCTYPE html PUBLIC …> <html> …
25. Resend same packets by Ruby www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
26.
27.
28.
29.
30.
31. Facility www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
32.
33. Mechanize www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development a = Mechanize.new do |agent| agent.user_agent_alias = 'Linux Firefox' end a.get('http://rubyconfchina.org/session/new') do |page| puts page.body =~ /Login/ # 1942 home_page = page.form_with(:action => '/session') do |f| f.login = 'flyerhzm' f.password = '…' end.click_button puts home_page.body =~ /Login/ # nil end
34. Test www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
35.
36. Test request www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Client Server GET /ht/sd.aspx?i=1 HTTP/1.1 Host: 221.176.31.39 … R fetion.com.cn SIP-C/4.0 F: 730020377 I: 1 Q: 1 R CN: 19D28D4978125CAA4F6E54277BA7D9EF CL: type="pc" ,version="3.6.2020" SIPP sid = 730020377 i = 0
37. Test request www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development @fetion.stubs(:sid).returns(730020377) @fetion.stubs(:i).returns(0) @fetion.stubs(:guid).returns("19D28D4978125CAA4F6E54277BA7D9EF") expected_sipc_message =<<-EOF R fetion.com.cn SIP-C/4.0 F: 730020377 I: 1 Q: 1 R CN: 19D28D4978125CAA4F6E54277BA7D9EF CL: type="pc" ,version="3.6.2020" SIPP EOF expected_sipc_message.gsub!("", "").chomp! SipcMessage.register_first(@fetion).should == expected_sipc_message Copied from Sniffer tool
38. Test response www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development Client Server HTTP/1.1 200 OK Server: nginx/0.5.32 … Set-Cookie: ssic=CBIOAAAm+FiuQgpcnFi; path=/ <?xml version="1.0" encoding="utf-8" ?> <results status-code="200"> <user uri="sip:730020377@fetion.com.cn;p=6907" /> </results>
39.
40. Test response www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development FakeWeb.register_uri( :get, 'https://uid.fetion.com.cn/ssiportal/SSIAppSignInV4.aspx? mobileno=15800681509&digest=79cd56b93f21298dc8ae9d26de1258e3d', :body => %Q|<?xml version="1.0" encoding="utf-8" ?><results status-code="200"><user uri="sip:730020377@fetion.com.cn;p=6907" /></results>|, :set_cookie => Q|ssic=CBIOAAAm+FiuQgpcnFi; path=/| ) @fetion.login @fetion.status_code.should == "200" @fetion.sid.should == "730020377" Copied from Sniffer tool
41. Test response www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development FakeWeb.register_uri( :get, 'https://uid.fetion.com.cn/ssiportal/SSIAppSignInV4.aspx? mobileno=15800681509&digest=79cd56b93f21298dc8ae9d26de1258e3d', :body => %Q|<?xml version="1.0" encoding="utf-8" ?><results status-code="401" desc="password error" />|, :status => ['401', 'password error'] ) lambda { @fetion.login }.should raise_exception(Fetion::PasswordError) Copied from Sniffer tool
42.
43. Performance www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development
44.
45.
46. Q&A Thank you www.ekohe.com Web Development & Graphic Design China Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development