cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 079b942d626991458ef283ab87e87c62b05eedb6 2129 bytes (raw)
$ git show HEAD:test/http_dav.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 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
34
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
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
require 'test/test_helper'
require 'net/http'
$stderr.sync = $stdout.sync = Thread.abort_on_exception = true

class TestHTTPDav < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-httpdav-test')
    @to_close = []
    @host = TEST_HOST
    srv = TCPServer.new(@host, 0)
    @port = srv.addr[1]
    srv.close
    @err = Tempfile.new("stderr")
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=500" ]
    vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
    @pid = fork {
      $stderr.reopen(@err)
      @err.close
      exec(*cmd)
    }
    @client = get_client
  end

  def teardown
    Process.kill(:QUIT, @pid) rescue nil
    _, status = Process.waitpid2(@pid)
    @to_close.each { |io| io.close unless io.closed? }
    FileUtils.rm_rf(@tmpdir)
    @err.rewind
    $stderr.write(@err.read)
    assert status.success?, status.inspect
  end

  def test_mkcol_delete
    Net::HTTP.start(@host, @port) do |http|
      assert ! File.directory?("#@tmpdir/foo")
      assert_equal 400, http.request(new_req(:Mkcol, "/foo")).code.to_i
      assert ! File.directory?("#@tmpdir/foo")

      Dir.mkdir("#@tmpdir/foo")
      File.open("#@tmpdir/foo/a", "w").close

      resp = http.request(new_req(:Delete, "/foo/a"))
      assert_equal 204, resp.code.to_i
      assert ! File.exist?("#@tmpdir/foo/a")

      resp = http.request(new_req(:Delete, "/foo/a"))
      assert_equal 404, resp.code.to_i

      resp = http.request(new_req(:Delete, "/foo"))
      assert_equal 403, resp.code.to_i

      resp = http.request(new_req(:Delete, "../foo"))
      assert_equal 400, resp.code.to_i

      resp = http.request(new_req(:Mkcol, "../foo"))
      assert_equal 400, resp.code.to_i

      resp = http.request(new_req(:Delete, "/"))
      assert_equal 403, resp.code.to_i
      resp = http.request(new_req(:Delete, "//"))
      assert_equal 403, resp.code.to_i
    end
  end
end

git clone https://yhbt.net/cmogstored.git