Ejemplo n.º 1
0
func TestParseEvent(t *testing.T) {
	// nil event
	data := []byte(`[8,"http://example.com/api/test",null]`)
	var msg eventMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(eventMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/test", msg.TopicURI)
	assert.Nil(t, msg.Event)

	// simple event
	data = []byte(`[8,"http://example.com/api/testing:thing","this is an event"]`)
	assert.Implements(t, (*json.Unmarshaler)(nil), new(eventMsg))
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/testing:thing", msg.TopicURI)
	assert.Equal(t, "this is an event", msg.Event)

	// complex event
	data = []byte(`[8,"http://www.example.com/doc#thing",{"name":"the test","value":17.3,"list":[1,2,3]}]`)
	assert.Implements(t, (*json.Unmarshaler)(nil), new(eventMsg))
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://www.example.com/doc#thing", msg.TopicURI)
	assert.Equal(t, "the test", msg.Event.(map[string]interface{})["name"])
	assert.Equal(t, 17.3, msg.Event.(map[string]interface{})["value"])
	assert.Equal(t, 3, msg.Event.(map[string]interface{})["list"].([]interface{})[2])
}
Ejemplo n.º 2
0
func TestParseCallResult(t *testing.T) {
	// null result
	data := []byte(`[3,"123456",null]`)
	var msg callResultMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(callResultMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "123456", msg.CallID)
	assert.Nil(t, msg.Result)

	// simple result
	data = []byte(`[3,"abcdefg","a cool result"]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "abcdefg", msg.CallID)
	assert.Equal(t, "a cool result", msg.Result)

	// complex result
	data = []byte(`[3,"asdf",{"name":"sally","value":43.1,"list":[2,4,6]}]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "asdf", msg.CallID)
	assert.Equal(t, "sally", msg.Result.(map[string]interface{})["name"])
	assert.Equal(t, 43.1, msg.Result.(map[string]interface{})["value"])
	assert.Equal(t, 6, msg.Result.(map[string]interface{})["list"].([]interface{})[2])
}
Ejemplo n.º 3
0
func TestParseUnsubscribe(t *testing.T) {
	data := []byte(`[6,"http://example.com/something"]`)
	var msg unsubscribeMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(unsubscribeMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/something", msg.TopicURI)
}
Ejemplo n.º 4
0
func TestParsePrefix(t *testing.T) {
	data := []byte(`[1,"prefix","http://www.example.com/api/start"]`)
	var msg prefixMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(prefixMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "prefix", msg.Prefix)
	assert.Equal(t, "http://www.example.com/api/start", msg.URI)
}
Ejemplo n.º 5
0
func TestParseWelcome(t *testing.T) {
	data := []byte(`[0,"12345678",1,"turnpike-0.1.0"]`)
	var msg welcomeMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(welcomeMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "12345678", msg.SessionId)
	assert.Equal(t, 1, msg.ProtocolVersion)
	assert.Equal(t, "turnpike-0.1.0", msg.ServerIdent)
}
Ejemplo n.º 6
0
func TestParseCallError(t *testing.T) {
	// generic error
	data := []byte(`[4,"1234","http://example.com/app/error#generic","there was an error"]`)
	var msg callErrorMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(callErrorMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "1234", msg.CallID)
	assert.Equal(t, "http://example.com/app/error#generic", msg.ErrorURI)
	assert.Equal(t, "there was an error", msg.ErrorDesc)
	assert.Nil(t, msg.ErrorDetails)

	// integer error details
	data = []byte(`[4,"asdf","http://example.com/error","integer error",4567]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "asdf", msg.CallID)
	assert.Equal(t, "http://example.com/error", msg.ErrorURI)
	assert.Equal(t, "integer error", msg.ErrorDesc)
	assert.Equal(t, 4567, msg.ErrorDetails)

	// complex error details
	data = []byte(`[4,"asd123","http://example.com/error","big error",{"name":"huge","value":9000,"list":[10,60]}]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "asd123", msg.CallID)
	assert.Equal(t, "http://example.com/error", msg.ErrorURI)
	assert.Equal(t, "big error", msg.ErrorDesc)
	assert.Equal(t, "huge", msg.ErrorDetails.(map[string]interface{})["name"])
	assert.Equal(t, 9000, msg.ErrorDetails.(map[string]interface{})["value"])
	assert.Equal(t, 60, msg.ErrorDetails.(map[string]interface{})["list"].([]interface{})[1])
}
Ejemplo n.º 7
0
func TestParseCall(t *testing.T) {
	// no call args
	data := []byte(`[2,"123456","http://example.com/testRPC"]`)
	var msg callMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(callMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "123456", msg.CallID)
	assert.Equal(t, "http://example.com/testRPC", msg.ProcURI)
	assert.Nil(t, msg.CallArgs)

	// simple call args
	data = []byte(`[2,"a1b2c3d4","http://example.com/dosomething/rpc","call arg"]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "a1b2c3d4", msg.CallID)
	assert.Equal(t, "http://example.com/dosomething/rpc", msg.ProcURI)
	assert.Equal(t, "call arg", msg.CallArgs[0].(string))

	// complex call args
	data = []byte(`[2,"1234","http://example.com/rpc",{"name":"george","value":14.98,"list":[1,3,5]},"astring"]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "1234", msg.CallID)
	assert.Equal(t, "http://example.com/rpc", msg.ProcURI)
	assert.Equal(t, "george", msg.CallArgs[0].(map[string]interface{})["name"])
	assert.Equal(t, 14.98, msg.CallArgs[0].(map[string]interface{})["value"])
	assert.Equal(t, 5, msg.CallArgs[0].(map[string]interface{})["list"].([]interface{})[2])
	assert.Equal(t, "astring", msg.CallArgs[1].(string))
}
Ejemplo n.º 8
0
func TestHTTP_Interface(t *testing.T) {

	assert.Implements(t, (*HTTPResponder)(nil), new(GowebHTTPResponder))

}
Ejemplo n.º 9
0
func TestParsePublish(t *testing.T) {
	// nill event
	data := []byte(`[7,"http://example.com/api/test",null]`)
	var msg publishMsg
	assert.Implements(t, (*json.Unmarshaler)(nil), new(publishMsg))
	err := json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/test", msg.TopicURI)
	assert.Nil(t, msg.Event)
	assert.False(t, msg.ExcludeMe)
	assert.Nil(t, msg.ExcludeList)
	assert.Nil(t, msg.EligibleList)

	// simple event
	data = []byte(`[7,"http://example.com/api/testing:thing","this is an event"]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/testing:thing", msg.TopicURI)
	assert.Equal(t, "this is an event", msg.Event)
	assert.False(t, msg.ExcludeMe)
	assert.Nil(t, msg.ExcludeList)
	assert.Nil(t, msg.EligibleList)

	// complex event
	data = []byte(`[7,"http://www.example.com/doc#thing",{"name":"the test","value":17.3,"list":[1,2,3]}]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://www.example.com/doc#thing", msg.TopicURI)
	assert.Equal(t, "the test", msg.Event.(map[string]interface{})["name"])
	assert.Equal(t, 17.3, msg.Event.(map[string]interface{})["value"])
	assert.Equal(t, 3, msg.Event.(map[string]interface{})["list"].([]interface{})[2])

	// with excludeMe
	data = []byte(`[7,"http://example.com/api/testing:thing","this is an event",true]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/testing:thing", msg.TopicURI)
	assert.Equal(t, "this is an event", msg.Event)
	assert.True(t, msg.ExcludeMe)

	// with exclude list
	data = []byte(`[7,"http://example.com/api/testing:thing","this is an event",["bob","john"]]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/testing:thing", msg.TopicURI)
	assert.Equal(t, "this is an event", msg.Event)
	assert.Equal(t, "bob", msg.ExcludeList[0])
	assert.Equal(t, "john", msg.ExcludeList[1])
	assert.Nil(t, msg.EligibleList)

	// with eligible list
	data = []byte(`[7,"http://example.com/api/testing:thing","this is an event",[],["sam","fred"]]`)
	err = json.Unmarshal(data, &msg)
	if err != nil {
		t.Errorf("error unmarshalling json: %s", err)
	}
	assert.Equal(t, "http://example.com/api/testing:thing", msg.TopicURI)
	assert.Equal(t, "sam", msg.EligibleList[0])
	assert.Equal(t, "fred", msg.EligibleList[1])
}
Ejemplo n.º 10
0
func TestPipe(t *testing.T) {

	assert.Implements(t, (*Handler)(nil), new(Pipe))

}
Ejemplo n.º 11
0
func TestAPI_Interface(t *testing.T) {

	assert.Implements(t, (*APIResponder)(nil), new(GowebAPIResponder))

}