blob: 51ea1ebbd4baf6a51c3741c1973cad0bd99ba05d [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apisix
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestItemUnmarshalJSON(t *testing.T) {
var items node
emptyData := `
{
"key": "test",
"nodes": {}
}
`
err := json.Unmarshal([]byte(emptyData), &items)
assert.Nil(t, err)
emptyData = `
{
"key": "test",
"nodes": {"a": "b", "c": "d"}
}
`
err = json.Unmarshal([]byte(emptyData), &items)
assert.Equal(t, err.Error(), "unexpected non-empty object")
emptyArray := `
{
"key": "test",
"nodes": []
}
`
err = json.Unmarshal([]byte(emptyArray), &items)
assert.Nil(t, err)
}
func TestItemConvertRoute(t *testing.T) {
item := &item{
Key: "/apisix/routes/001",
Value: json.RawMessage(`
{
"upstream_id": "13",
"host": "foo.com",
"uri": "/shop/133/details",
"name": "unknown",
"methods": ["GET", "POST"]
}
`),
}
r, err := item.route()
assert.Nil(t, err)
assert.Equal(t, r.UpstreamId, "13")
assert.Equal(t, r.Host, "foo.com")
assert.Equal(t, r.Uri, "/shop/133/details")
assert.Equal(t, r.Methods[0], "GET")
assert.Equal(t, r.Methods[1], "POST")
assert.Equal(t, r.Name, "unknown")
}