func TestParseApiConfigConvertHttps(t *testing.T) {
	configManager := newApiConfigManager()
	descriptor := &endpoints.ApiDescriptor{
		Name:    "guestbook_api",
		Version: "X",
		Root:    "https://localhost/_ah/api",
		Methods: make(map[string]*endpoints.ApiMethod),
	}
	descriptor.Adapter.Bns = "https://localhost/_ah/spi"
	descriptor.Adapter.Type = "lily"
	config, _ := json.Marshal(descriptor)
	items, _ := json.Marshal(map[string]interface{}{
		"items": []string{string(config)},
	})
	configManager.parseApiConfigResponse(string(items))
	key := lookupKey{"guestbook_api", "X"}
	assert.Equal(t, "http://localhost/_ah/spi", configManager.configs()[key].Adapter.Bns)
	assert.Equal(t, "http://localhost/_ah/api", configManager.configs()[key].Root)
}
Test that the parsed API config has switched HTTPS to HTTP.
package server
import (
	"encoding/json"
	"fmt"
	"github.com/rwl/go-endpoints/endpoints"
	"github.com/stretchr/testify/assert"
	"testing"
)
func TestParseApiConfigEmptyResponse(t *testing.T) {
	configManager := newApiConfigManager()
	configManager.parseApiConfigResponse("")
	actualMethod := configManager.lookupRpcMethod("guestbook_api.get", "v1")
	assert.Nil(t, actualMethod)
}
func TestParseApiConfigInvalidResponse(t *testing.T) {
	configManager := newApiConfigManager()
	configManager.parseApiConfigResponse(`{"name": "foo"}`)
	actualMethod := configManager.lookupRpcMethod("guestbook_api.get", "v1")
	assert.Nil(t, actualMethod)
}
func TestParseApiConfig(t *testing.T) {
	configManager := newApiConfigManager()
	fakeMethod := &endpoints.ApiMethod{
		HttpMethod: "GET",
		Path:       "greetings/{gid}",
		RosyMethod: "baz.bim",
	}
	config, _ := json.Marshal(&endpoints.ApiDescriptor{
		Name:    "guestbook_api",
		Version: "X",
		Methods: map[string]*endpoints.ApiMethod{
			"guestbook_api.foo.bar": fakeMethod,
		},
	})
	items, _ := json.Marshal(map[string]interface{}{
		"items": []string{string(config)},
	})
	err := configManager.parseApiConfigResponse(string(items))
	assert.NoError(t, err)
	actualMethod := configManager.lookupRpcMethod("guestbook_api.foo.bar", "X")
	assert.Equal(t, fakeMethod, actualMethod)
}
func TestParseApiConfigOrderLength(t *testing.T) {
	configManager := newApiConfigManager()
	methods := map[string]*endpoints.ApiMethod{
		"guestbook_api.foo.bar": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greetings/{gid}",
			RosyMethod: "baz.bim",
		},
		"guestbook_api.list": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greetings",
			RosyMethod: "greetings.list",
		},
		"guestbook_api.f3": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greetings/{gid}/sender/property/blah",
			RosyMethod: "greetings.f3",
		},
		"guestbook_api.shortgreet": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greet",
			RosyMethod: "greetings.short_greeting",
		},
	}
	config, err := json.Marshal(&endpoints.ApiDescriptor{
		Name:    "guestbook_api",
		Version: "X",
		Methods: methods,
	})
	assert.NoError(t, err)
	items, err := json.Marshal(map[string]interface{}{
		"items": []string{string(config)},
	})
	assert.NoError(t, err)
	err = configManager.parseApiConfigResponse(string(items))
	assert.NoError(t, err)
	for methodName, _ := range methods {
		assert.NotNil(t, configManager.lookupRpcMethod(methodName, "X"))
	}
	var mn string
	mn, _, _ = configManager.lookupRestMethod("guestbook_api/X/greetings", "GET")
	assert.Equal(t, mn, "guestbook_api.list")
	mn, _, _ = configManager.lookupRestMethod("guestbook_api/X/greetings/1", "GET")
	assert.Equal(t, mn, "guestbook_api.foo.bar")
	mn, _, _ = configManager.lookupRestMethod("guestbook_api/X/greetings/2/sender/property/blah", "GET")
	assert.Equal(t, mn, "guestbook_api.f3")
	mn, _, _ = configManager.lookupRestMethod("guestbook_api/X/greet", "GET")
	assert.Equal(t, mn, "guestbook_api.shortgreet")
}
func TestSortMethods1(t *testing.T) {
	methods := map[string]*endpoints.ApiMethod{
		"name1": &endpoints.ApiMethod{
			HttpMethod: "POST",
			Path:       "greetings",
		},
		"name2": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greetings",
		},
		"name3": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "short/but/many/constants",
		},
		"name4": &endpoints.ApiMethod{
			HttpMethod: "",
			Path:       "greetings",
		},
		"name5": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greetings/{gid}",
		},
		"name6": &endpoints.ApiMethod{
			HttpMethod: "PUT",
			Path:       "greetings/{gid}",
		},
		"name7": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "a/b/{var}/{var2}",
		},
	}
	sortedMethods := sortMethods(methods)
	expectedMethods := []*methodInfo{
		&methodInfo{
			"name3",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "short/but/many/constants",
			},
		},
		&methodInfo{
			"name7",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "a/b/{var}/{var2}",
			},
		},
		&methodInfo{
			"name4",
			&endpoints.ApiMethod{
				HttpMethod: "",
				Path:       "greetings",
			},
		},
		&methodInfo{
			"name2",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "greetings",
			},
		},
		&methodInfo{
			"name1",
			&endpoints.ApiMethod{
				HttpMethod: "POST",
				Path:       "greetings",
			},
		},
		&methodInfo{
			"name5",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "greetings/{gid}",
			},
		},
		&methodInfo{
			"name6",
			&endpoints.ApiMethod{
				HttpMethod: "PUT",
				Path:       "greetings/{gid}",
			},
		},
	}
	assert.Equal(t, expectedMethods, sortedMethods)
}
func TestSortMethods2(t *testing.T) {
	methods := map[string]*endpoints.ApiMethod{
		"name1": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "abcdefghi",
		},
		"name2": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "foo",
		},
		"name3": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "greetings",
		},
		"name4": &endpoints.ApiMethod{
			HttpMethod: "POST",
			Path:       "bar",
		},
		"name5": &endpoints.ApiMethod{
			HttpMethod: "GET",
			Path:       "baz",
		},
		"name6": &endpoints.ApiMethod{
			HttpMethod: "PUT",
			Path:       "baz",
		},
		"name7": &endpoints.ApiMethod{
			HttpMethod: "DELETE",
			Path:       "baz",
		},
	}
	sortedMethods := sortMethods(methods)
	expectedMethods := []*methodInfo{
		&methodInfo{
			"name1",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "abcdefghi",
			},
		},
		&methodInfo{
			"name4",
			&endpoints.ApiMethod{
				HttpMethod: "POST",
				Path:       "bar",
			},
		},
		&methodInfo{
			"name7",
			&endpoints.ApiMethod{
				HttpMethod: "DELETE",
				Path:       "baz",
			},
		},
		&methodInfo{
			"name5",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "baz",
			},
		},
		&methodInfo{
			"name6",
			&endpoints.ApiMethod{
				HttpMethod: "PUT",
				Path:       "baz",
			},
		},
		&methodInfo{
			"name2",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "foo",
			},
		},
		&methodInfo{
			"name3",
			&endpoints.ApiMethod{
				HttpMethod: "GET",
				Path:       "greetings",
			},
		},
	}
	assert.Equal(t, expectedMethods, sortedMethods)
}
func TestParseApiConfigInvalidApiConfig(t *testing.T) {
	configManager := newApiConfigManager()
	fakeMethod := &endpoints.ApiMethod{
		HttpMethod: "GET",
		Path:       "greetings/{gid}",
		RosyMethod: "baz.bim",
	}
	config, _ := json.Marshal(&endpoints.ApiDescriptor{
		Name:    "guestbook_api",
		Version: "X",
		Methods: map[string]*endpoints.ApiMethod{
			"guestbook_api.foo.bar": fakeMethod,
		},
	})
	config2 := "{"  
	items, _ := json.Marshal(map[string]interface{}{
		"items": []string{string(config), string(config2)},
	})
	configManager.parseApiConfigResponse(string(items))
	actualMethod := configManager.lookupRpcMethod("guestbook_api.foo.bar", "X")
	assert.Equal(t, fakeMethod, actualMethod)
}