func TestEmptyTest(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	resp, err := http.Get(ts.URL + "/_ah/api/test_service/v1/emptytest")
	assert.Equal(t, 200, resp.StatusCode)
	assert.Equal(t, "16", resp.Header.Get("Content-Length"))
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	assert.Equal(t, `{
  "text": ""
}`, string(content))
}
Test that an empty response that should have an object returns 200.
package server
import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"github.com/stretchr/testify/assert"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"
)
func TestRestGet(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	resp, err := http.Get(ts.URL + "/_ah/api/test_service/v1/test")
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	body, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson map[string]interface{}
	err = json.Unmarshal(body, &responseJson)
	assert.NoError(t, err)
	expected := map[string]interface{}{"text": "Test response"}
	assert.Equal(t, expected, responseJson)
}
func TestRestPost(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	body, err := json.Marshal(map[string]interface{}{
		"name":   "MyName",
		"number": 23,
	})
	assert.NoError(t, err)
	resp, err := http.Post(ts.URL+"/_ah/api/test_service/v1/t2path",
		"application/json", bytes.NewBuffer(body))
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson map[string]interface{}
	err = json.Unmarshal(content, &responseJson)
	assert.NoError(t, err)
	expected := map[string]interface{}{"text": "MyName 23"}
	assert.Equal(t, expected, responseJson)
}
func TestCors(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	client := &http.Client{}
	req, err := http.NewRequest("GET",
		ts.URL+"/_ah/api/test_service/v1/test", nil)
	req.Header.Set("Origin", "test.com")
	req.Header.Set("Access-control-request-method", "GET")
	req.Header.Set("Access-Control-Request-Headers", "Date,Expires")
	resp, err := client.Do(req)
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get(corsHeaderAllowOrigin), "test.com")
	assert.Equal(t, resp.Header.Get(corsHeaderAllowHeaders), "Date,Expires")
	allowed := strings.Split(resp.Header.Get(corsHeaderAllowMethods), ",")
	for _, header := range allowed {
		if header == "GET" {
			return
		}
	}
	t.Fail()
}
func TestRpc(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	body, err := json.Marshal([]map[string]interface{}{
		map[string]interface{}{
			"jsonrpc": "2.0",
			"id":      "gapiRpc",
			"method":  "test_service.t2name",
			"params": map[string]interface{}{
				"name":   "MyName",
				"number": 23,
			},
			"apiVersion": "v1",
		},
	})
	assert.NoError(t, err)
	resp, err := http.Post(ts.URL+"/_ah/api/rpc",
		"application-rpc", bytes.NewBuffer(body))
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson interface{}
	err = json.Unmarshal(content, &responseJson)
	assert.NoError(t, err)
	assert.Equal(t, []interface{}{
		map[string]interface{}{
			"result": map[string]interface{}{
				"text": "MyName 23",
			},
			"id": "gapiRpc",
		},
	}, responseJson)
}
func TestEchoDatetimeMessage(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	body, err := json.Marshal(map[string]interface{}{
		"milliseconds":     5000,
		"time_zone_offset": 60,
	})
	assert.NoError(t, err)
	resp, err := http.Post(ts.URL+"/_ah/api/test_service/v1/echodatemessage",
		"application/json", bytes.NewBuffer(body))
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson map[string]interface{}
	err = json.Unmarshal(content, &responseJson)
	assert.NoError(t, err)
	expected := map[string]interface{}{
		"milliseconds":     5000,
		"time_zone_offset": 60,
	}
	assert.Equal(t, expected, responseJson)
}
func TestEchoDatetimeField(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	bodyJson := map[string]interface{}{
		"date": "2013-03-13T15:29:37.883+08:00",
	}
	body, err := json.Marshal(bodyJson)
	assert.NoError(t, err)
	resp, err := http.Post(ts.URL+"/_ah/api/test_service/v1/echodatetimefield",
		"application/json", bytes.NewBuffer(body))
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson map[string]interface{}
	err = json.Unmarshal(content, &responseJson)
	assert.NoError(t, err)
	assert.Equal(t, bodyJson, responseJson)
}
func TestIncrementIntegers(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	bodyJson := map[string]interface{}{
		"var_int32": 100,
		"var_int64": "1000",
		"var_repeated_int64": []string{
			"10", "11", "900",
		},
		"var_uint64": "4320",
	}
	body, err := json.Marshal(bodyJson)
	assert.NoError(t, err)
	resp, err := http.Post(ts.URL+"/_ah/api/test_service/v1/incrementintegers",
		"application/json", bytes.NewBuffer(body))
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson map[string]interface{}
	err = json.Unmarshal(content, &responseJson)
	assert.NoError(t, err)
	expectedResponse := map[string]interface{}{
		"var_int32": 101,
		"var_int64": "1001",
		"var_repeated_int64": []interface{}{
			"11", "12", "901",
		},
		"var_uint64": "4321",
	}
	assert.Equal(t, expectedResponse, responseJson)
}
func TestEchoBytes(t *testing.T) {
	ts := initTestApi(t)
	defer ts.Close()
	value := []byte("This is a test of a message encoded as a BytesField.01234\000\001")
	bytesValue := base64.URLEncoding.EncodeToString(value)
	bodyJson := map[string]interface{}{"bytes_value": bytesValue}
	body, err := json.Marshal(bodyJson)
	assert.NoError(t, err)
	resp, err := http.Post(ts.URL+"/_ah/api/test_service/v1/echobytes",
		"application/json", bytes.NewBuffer(body))
	assert.NoError(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
	content, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)
	var responseJson map[string]interface{}
	err = json.Unmarshal(content, &responseJson)
	assert.NoError(t, err)
	assert.Equal(t, responseJson, bodyJson)
	dec, err := base64.URLEncoding.DecodeString(bytesValue)
	assert.NoError(t, err)
	assert.Equal(t, value, dec)
}