Module: RubyJmeter::Parser

Included in:
ExtendedDSL
Defined in:
lib/ruby-jmeter/helpers/parser.rb

Instance Method Summary collapse

Instance Method Details

#files(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby-jmeter/helpers/parser.rb', line 88

def files(params)
  files = params.delete(:files)
  return if files.empty?
  x = Nokogiri::XML::Builder.new do |b|
    b.elementProp(name: "HTTPsampler.Files", elementType: "HTTPFileArgs") {
      b.collectionProp(name: "HTTPFileArgs.files") {
        files.each do |f|
          b.elementProp(name: f[:path], elementType: "HTTPFileArg") {
            b.stringProp f[:path] || '' , name: "File.path"
            b.stringProp f[:paramname] || '' , name: "File.paramname"
            b.stringProp f[:mimetype] || '' , name: "File.mimetype"
          }
        end
      }
    }
  end
  params[:update_at_xpath] ||= []
  params[:update_at_xpath] << {
    :xpath => '//HTTPSamplerProxy',
    :value => x.doc.root
  }
end

#fill_in(params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby-jmeter/helpers/parser.rb', line 44

def fill_in(params)
  params[:update_at_xpath] ||= []
  params[:update_at_xpath] = params[:fill_in].
    each_with_object(params[:update_at_xpath]) do |(name, values), memo|
       Array(values).each do |value|
        memo << {
          :xpath => '//collectionProp',
          :value => Nokogiri::XML(<<-EOF.strip_heredoc).children
            <elementProp name="#{name}" elementType="HTTPArgument">
              <boolProp name="HTTPArgument.always_encode">#{params[:always_encode] ? 'true' : false}</boolProp>
              <stringProp name="Argument.value">#{value}</stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
              <boolProp name="HTTPArgument.use_equals">true</boolProp>
              <stringProp name="Argument.name">#{name}</stringProp>
            </elementProp>
            EOF
        }
      end
    end
  params.delete(:fill_in)
end

#parse_http_request(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby-jmeter/helpers/parser.rb', line 4

def parse_http_request(params)
  if params[:raw_path]
    params[:path] = params[:url]
    parse_url(params)
  else
    parse_url(params)
    params[:fill_in] ||= {}
    params[:params] && params[:params].split('&').each do |param|
      name,value = param.split('=')
      params[:fill_in][name] ||= []
      params[:fill_in][name] << value
    end
  end

  fill_in(params) if params.has_key?(:fill_in)
  raw_body(params) if params.has_key?(:raw_body)
  files(params) if params.has_key?(:files)
end

#parse_test_type(params) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby-jmeter/helpers/parser.rb', line 111

def parse_test_type(params)
  case params.keys.first.to_s
  when 'contains'
    2
  when 'not-contains'
    6
  when 'matches'
    1
  when 'not-matches'
    5
  when 'equals'
    8
  when 'not-equals'
    12
  when 'substring'
    16
  when 'not-substring'
    20
  else
    2
  end
end

#parse_uri(uri) ⇒ Object



38
39
40
41
42
# File 'lib/ruby-jmeter/helpers/parser.rb', line 38

def parse_uri(uri)
  URI.parse(URI::encode(uri)).scheme.nil? ?
    URI.parse(URI::encode("http://#{uri}")) :
    URI.parse(URI::encode(uri))
end

#parse_url(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby-jmeter/helpers/parser.rb', line 23

def parse_url(params)
  return if params[:url].empty?
  if params[:url] =~ /https?:\/\/\$/ || params[:url][0] == '$'
    params[:path] = params[:url] # special case for named expressions
  else
    uri = parse_uri(params[:url])
    params[:port]     ||= uri.port unless URI.parse(URI::encode(params[:url])).scheme.nil?
    params[:protocol] ||= uri.scheme unless URI.parse(URI::encode(params[:url])).scheme.nil?
    params[:domain]   ||= uri.host
    params[:path]     ||= uri.path && URI::decode(uri.path)
    params[:params]   ||= uri.query && URI::decode(uri.query)
  end
  params.delete(:url)
end

#raw_body(params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby-jmeter/helpers/parser.rb', line 66

def raw_body(params)
  params[:update_at_xpath] ||= []
  params[:update_at_xpath] << {
    :xpath => '//HTTPSamplerProxy',
    :value => Nokogiri::XML(<<-EOF.strip_heredoc).children
      <boolProp name="HTTPSampler.postBodyRaw">true</boolProp>
      EOF
  }

  params[:update_at_xpath] << {
    :xpath => '//collectionProp',
    :value => Nokogiri::XML(<<-EOF.strip_heredoc).children
      <elementProp name="" elementType="HTTPArgument">
        <boolProp name="HTTPArgument.always_encode">false</boolProp>
        <stringProp name="Argument.value">#{params[:raw_body].encode(:xml => :text)}</stringProp>
        <stringProp name="Argument.metadata">=</stringProp>
      </elementProp>
      EOF
  }
  params.delete(:raw_body)
end