Class: Smokegraphy::TestCase

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/smokegraphy/test_case.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.suiteObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smokegraphy/test_case.rb', line 11

def self.suite
  suite = super
  
  @@config = YAML.load_file('config.yaml')
  @@rc = ResourceController.build(@@config)
  @@http_server = @@config["http"]["server"]

  def suite.run(*args)
    if (name != "Smokegraphy::TestCase") then
      @@rc.init
      super
      @@rc.close
    end
  end

  suite
end

Instance Method Details

#deploy(filename) ⇒ Object



56
57
58
# File 'lib/smokegraphy/test_case.rb', line 56

def deploy(filename)
  @@rc.transfer_source_file(filename, @suite)
end

#prepareObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/smokegraphy/test_case.rb', line 44

def prepare
  @@rc.transfer_source_file(@filename, @suite)
  suffix = @filename.gsub(/[^\.]*\.([a-z0-9]*)/, "\\1")
  @@rc.add_execute_permission(@filename) if suffix == "cgi"

  # no warning
  begin
    @@rc.transfer_source_file("#{@filename.gsub(/\./, "_")}.htaccess", @suite, ".htaccess")
  rescue => ever
  end
end

#setupObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/smokegraphy/test_case.rb', line 29

def setup
  @log = Logger.new("test.log")
  @log.datetime_format = "%Y-%m-%d %H:%M:%S"

  name.match(/(.*)\((.*)\)/)
  @testname = $1
  @suite = $2.gsub(/([A-Z])/, "_\\1").downcase.gsub(/^_/,"").gsub(/^test_/,"")
  @filename = @testname.gsub(/test_(.*)_([^_]*)$/, "\\1.\\2").gsub(/_html/,".html")
  prepare
end

#simple_test(options = {}) ⇒ Object



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
# File 'lib/smokegraphy/test_case.rb', line 60

def simple_test(options = {})

  @@rc.transfer_source_file("#{options[:htaccess]}.htaccess", @suite, ".htaccess") if (options[:htaccess])
  
  begin

    # prepare request object
    uri = "/"
    if @@config["http"]["base_path"] then
      uri << @@config["http"]["base_path"]
      uri << "/"
    end

    uri << (options[:request_uri] ? options[:request_uri] : @filename)
      
    uri << "?%s" % options[:query].map { |k,v| "%s=%s" % [k,v] }.sort.join("&") if options[:query]

    if (options[:method] == :post) then
      req = Net::HTTP::Post.new(uri)
      req.body = options[:post_data].map { |k,v| "%s=%s" % [k,v] }.sort.join("&") if options[:post_data]
      req["Request-Method"] = "POST"
      req["Content-Length"] = req.body.size
    else
      req = Net::HTTP::Get.new(uri)
    end

    # setup request header       
    options[:request_header].each {|k,v| req[k] = v } if (options[:request_header] != nil)
    req["Host"] = @@http_server
    Net::HTTP.start(@@http_server, 80) do |http|

      # execute http access
      res = http.request(req)

      # check status code.
      assert_equal(options[:code] ? options[:code].to_s : "200", res.code,
                   "status code check failed. [filename: #{@filename}]")

      # check body
      if (res.code == "200" && (!block_given? || options[:expected] == true)) then
        expected = "expected/#{@suite}/#{@filename.gsub(/\./, "_")}.result"
        assert_equal(File.new(File.exist?(expected) ? expected : "source/#{@suite}/#{@filename}").read, res.body) 
      end

      # block process
      yield res if block_given?

    end

  rescue => evar
    @log.info(evar)
    result = "Failed"

  else
    result = "Success"

  ensure
    @log.info("test #{@suite}:#{@filename}: #{result} [#{options.keys.join(", ")}]")
    
    @@rc.cleanup(".htaccess") if options[:htaccess]

  end

  raise evar  if (evar) 
end

#teardownObject



40
41
42
# File 'lib/smokegraphy/test_case.rb', line 40

def teardown
  # @@rc.cleanup
end