Class: Client

Inherits:
Application show all
Defined in:
lib/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/client.rb', line 6

def initialize
  # Setting IP of server
  print "Input IP address of servrer: ".green
  @ip = $stdin.gets.chomp
  @ip = 'localhost' if blank? @ip

  # Setting PORT of server
  print "Input Port of servrer: ".green
  @port = $stdin.gets.chomp
  @port = '5899' if blank? @port

  # Setting eterations count
  print "Input eterations count: ".green
  @each_count = $stdin.gets.chomp.to_i
  @each_count = 1 if @each_count.zero?

  # Setting eterations count
  print "Input threads count: ".green
  @threads_count = $stdin.gets.chomp.to_i
  @threads_count = 1 if @threads_count.zero?

  # Building link
  @uri = "http://#{@ip}:#{@port}"

  # Thread and time arrays
  @req_times = []
  @threads = []
end

Instance Method Details

#resultObject



145
146
147
# File 'lib/client.rb', line 145

def result
  @req_times
end

#testObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/client.rb', line 35

def test
  @threads_count.times do
    @threads << Thread.new do
      @each_count.times do
        # Raw root GET
        t = Time.now
        response = Curl.get(@uri)
        @req_times << Time.now - t
        if response.status == '200 OK'
          print '.'.green
        else
          print '!'.red
        end

        # Root POST with correct params
        t = Time.now
        response = Curl.post(@uri, { name: 'Oleg', github_nickname: 'sorefull' })
        @req_times << Time.now - t
        if response.status == '202 Accepted'
          print '.'.green
        else
          print '!'.red
        end

        # Root POST with wrong params
        t = Time.now
        response = Curl.post(@uri, { name: '', github_nickname: '' })
        @req_times << Time.now - t
        if response.status == '401 Unauthorized'
          print '.'.green
        else
          print '!'.red
        end

        # Lorem GET with body check
        t = Time.now
        response = Curl.get(@uri + '/lorem')
        @req_times << Time.now - t
        if response.body == LoremIpsum.lorem
          print '.'.green
        else
          print '!'.red
        end

        # Lorem GET with params
        t = Time.now
        response = Curl.get(@uri + '/lorem', { length: 10 })
        @req_times << Time.now - t
        if response.body == LoremIpsum.lorem[0..10]
          print '.'.green
        else
          print '!'.red
        end

        # Cookie GET with correct cookies
        t = Time.now
        response = Curl.get(@uri + '/cookie') do |http|
          http.headers['Cookie'] = 'private=asdfg; public=gijj'
        end
        @req_times << Time.now - t
        if response.status == '200 OK'
          print '.'.green
        else
          print '!'.red
        end

        # Cookie GET with wrong cookies
        t = Time.now
        response = Curl.get(@uri + '/cookie') do |http|
          http.headers['Cookie'] = 'private=wrong; public=wrong'
        end
        @req_times << Time.now - t
        if response.status == '401 Unauthorized'
          print '.'.green
        else
          print '!'.red
        end

        # File GET
        file_name = '/file.txt'
        t = Time.now
        response = Curl.get(@uri + file_name)
        @req_times << Time.now - t
        file = File.open("./files#{file_name}", 'r')
        if response.body == file.read && response.status == '200 OK'
          print '.'.green
        else
          print '!'.red
        end

        # File POST
        file_name = 'image.jpeg'
        response = Curl::Easy.new(@uri + '/file')
        response.multipart_form_post = true
        t = Time.now
        response.http_post(Curl::PostField.file('uploaded_image', "./files/#{file_name}"))
        @req_times << Time.now - t
        if response.status == '200 OK'
          print '.'.green
        else
          print '!'.red
        end
      end
    end
  end

  # Joining threads
  @threads.each {|thread| thread.join}
end