~dave-cheney/goamz/004-send-409-conflict

« back to all changes in this revision

Viewing changes to s3/s3test/server.go

  • Committer: Dave Cheney
  • Date: 2012-12-10 23:38:27 UTC
  • Revision ID: david.cheney@canonical.com-20121210233827-e7cafxl8yhbmd53l
responding to review feedback

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
        reqId string
41
41
}
42
42
 
 
43
// Config controls the internal behaviour of the Server. A nil config is the default
 
44
// and behaves as if all configurations assume their default behaviour. Once passed
 
45
// to NewServer, the configuration must not be modified.
 
46
type Config struct {
 
47
        // Send409Conflict controls how the Server will respond to calls to PUT on a
 
48
        // previously existing bucket. The default is false, and corresponds to the
 
49
        // us-east-1 s3 enpoint. Setting this value to true emulates the behaviour of
 
50
        // all other regions.
 
51
        // http://docs.amazonwebservices.com/AmazonS3/latest/API/ErrorResponses.html
 
52
        Send409Conflict bool
 
53
}
 
54
 
 
55
func (c *Config) send409Conflict() bool {
 
56
        if c != nil {
 
57
                return c.Send409Conflict
 
58
        }
 
59
        return false
 
60
}
 
61
 
43
62
// Server is a fake S3 server for testing purposes.
44
63
// All of the data for the server is kept in memory.
45
64
type Server struct {
48
67
        listener net.Listener
49
68
        mu       sync.Mutex
50
69
        buckets  map[string]*bucket
51
 
        // should this server emulate the permissive behavior
52
 
        // of us-east-1, or default to the strict behavior of
53
 
        // the other regions.
54
 
        emulateUSEast1 bool
 
70
        config   *Config
55
71
}
56
72
 
57
73
type bucket struct {
79
95
        delete(a *action) interface{}
80
96
}
81
97
 
82
 
func NewServer(emulateUSEast1 bool) (*Server, error) {
 
98
func NewServer(config *Config) (*Server, error) {
83
99
        l, err := net.Listen("tcp", "localhost:0")
84
100
        if err != nil {
85
101
                return nil, fmt.Errorf("cannot listen on localhost: %v", err)
86
102
        }
87
103
        srv := &Server{
88
 
                listener:       l,
89
 
                url:            "http://" + l.Addr().String(),
90
 
                buckets:        make(map[string]*bucket),
91
 
                emulateUSEast1: emulateUSEast1,
 
104
                listener: l,
 
105
                url:      "http://" + l.Addr().String(),
 
106
                buckets:  make(map[string]*bucket),
 
107
                config:   config,
92
108
        }
93
109
        go http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
94
110
                srv.serveHTTP(w, req)
408
424
                a.srv.buckets[r.name] = r.bucket
409
425
                created = true
410
426
        }
411
 
        if !created && !a.srv.emulateUSEast1 {
412
 
                // calling PUT bucket on an existing bucket outside us-east-1 is an error
413
 
                // http://docs.amazonwebservices.com/AmazonS3/latest/API/ErrorResponses.html
 
427
        if !created && a.srv.config.send409Conflict() {
414
428
                fatalf(409, "BucketAlreadyOwnedByYou", "Your previous request to create the named bucket succeeded and you already own it.")
415
429
        }
416
430
        r.bucket.acl = s3.ACL(a.req.Header.Get("x-amz-acl"))