forked from contentful-labs/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_test.go
106 lines (87 loc) · 2.11 KB
/
entry_test.go
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
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
package contentful
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func ExampleEntriesService_Upsert_create() {
cma := NewCMA("cma-token")
entry := &Entry{
Sys: &Sys{
ID: "MyEntry",
ContentType: &ContentType{
Sys: &Sys{
ID: "MyContentType",
},
},
},
Fields: map[string]interface{}{
"Description": map[string]string{
"en-US": "Some example content...",
},
},
}
err := cma.Entries.Upsert("space-id", entry)
if err != nil {
log.Fatal(err)
}
}
func ExampleEntryService_Upsert_update() {
cma := NewCMA("cma-token")
entry, err := cma.Entries.Get("space-id", "entry-id")
if err != nil {
log.Fatal(err)
}
entry.Fields["Description"] = map[string]interface{}{
"en-US": "modified entry content",
}
err = cma.Entries.Upsert("space-id", entry)
if err != nil {
log.Fatal(err)
}
}
func TestEntrySaveForCreate(t *testing.T) {
var err error
assert := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(r.Method, "POST")
assert.Equal(r.RequestURI, "/spaces/"+spaceID+"/environments/master/entries")
assert.Equal(r.Header["X-Contentful-Content-Type"], []string{"MyContentType"})
checkHeaders(r, assert)
var payload map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&payload)
assert.Nil(err)
assert.NotNil(payload["fields"])
fields := payload["fields"].(map[string]interface{})
assert.Equal(fields["Description"], map[string]interface{}{"en-US": "Some test content..."})
w.WriteHeader(201)
fmt.Fprintln(w, string(readTestData("entry_3.json")))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
entry := &Entry{
Sys: &Sys{
ContentType: &ContentType{
Sys: &Sys{
ID: "MyContentType",
},
},
},
Fields: map[string]interface{}{
"Description": map[string]string{
"en-US": "Some test content...",
},
},
}
err = cma.Entries.Upsert("id1", entry)
assert.Nil(err)
assert.Equal("foocat", entry.Sys.ID)
}