github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/server/v2/tests/post_handler_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/coreos/etcd/server"
     9  	"github.com/coreos/etcd/tests"
    10  	"github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
    11  )
    12  
    13  // Ensures a unique value is added to the key's children.
    14  //
    15  //   $ curl -X POST localhost:4001/v2/keys/foo/bar
    16  //   $ curl -X POST localhost:4001/v2/keys/foo/bar
    17  //   $ curl -X POST localhost:4001/v2/keys/foo/baz
    18  //
    19  func TestV2CreateUnique(t *testing.T) {
    20  	tests.RunServer(func(s *server.Server) {
    21  		// POST should add index to list.
    22  		fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
    23  		resp, _ := tests.PostForm(fullURL, nil)
    24  		assert.Equal(t, resp.StatusCode, http.StatusCreated)
    25  		body := tests.ReadBodyJSON(resp)
    26  		assert.Equal(t, body["action"], "create", "")
    27  
    28  		node := body["node"].(map[string]interface{})
    29  		assert.Equal(t, node["key"], "/foo/bar/2", "")
    30  		assert.Nil(t, node["dir"], "")
    31  		assert.Equal(t, node["modifiedIndex"], 2, "")
    32  
    33  		// Second POST should add next index to list.
    34  		resp, _ = tests.PostForm(fullURL, nil)
    35  		assert.Equal(t, resp.StatusCode, http.StatusCreated)
    36  		body = tests.ReadBodyJSON(resp)
    37  
    38  		node = body["node"].(map[string]interface{})
    39  		assert.Equal(t, node["key"], "/foo/bar/3", "")
    40  
    41  		// POST to a different key should add index to that list.
    42  		resp, _ = tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/baz"), nil)
    43  		assert.Equal(t, resp.StatusCode, http.StatusCreated)
    44  		body = tests.ReadBodyJSON(resp)
    45  
    46  		node = body["node"].(map[string]interface{})
    47  		assert.Equal(t, node["key"], "/foo/baz/4", "")
    48  	})
    49  }