Beispiel #1
0
func init() {
	setup()
	var wrap data.Wrapper
	var msg data.Message
	wrap.Action = "Send"
	wrap.QueueID = "main"
	msg.Uuid = uuid.NewRandom().String()
	msg.Body = "Some exciting stuff"
	msg.Created = time.Now().Unix()
	msg.Persistent = true
	wrap.Message = msg
}
Beispiel #2
0
func TestApiMessageStatus(t *testing.T) {
	var buf bytes.Buffer

	// Set up another test wrapper
	var tw data.Wrapper
	var msg data.Message
	msg.Uuid = uuid.NewRandom().String()
	msg.Body = "another test thing"
	msg.Route = []string{"default"}
	tw.Message = msg

	// Send the wrapper using the client
	cc := client.NewConnection(testcontext)
	err := cc.SendWrapper(&tw)
	if err != nil {
		log.Printf("Error using client to send wrapper : %v\n", err)
		t.Fatalf("%v\n", err)
	}

	// Check the message status using the web api
	var wr webmanager.StatusRequest
	wr.IsMessage = true
	wr.MessageUuid = msg.Uuid

	if err := json.NewEncoder(&buf).Encode(&wr); err != nil {
		t.Fatalf("%v\n", err)
	}

	resp, err := http.Post("http://127.0.0.1:8080/api/status", "application/json", &buf)
	if err != nil {
		t.Fatalf("%v\n", err)
	}
	if resp.StatusCode == 400 {
		t.Fatalf("Server returned 400 when checking message")
	}
	defer resp.Body.Close()
	var msr webmanager.MessageStatusResp
	if err := json.NewDecoder(resp.Body).Decode(&msr); err != nil {
		if err.Error() != "EOF" {
			t.Fatalf("%v\n", err)
		}
	}
	if msr.Status != "Received" {
		t.Fatalf("Message status response has the wrong message status")
	}
}
Beispiel #3
0
// DeliverMessage takes POST JSON data containing a message wrapper which is then processed.
func DeliverMessage(w http.ResponseWriter, r *http.Request, context *data.Context) {

	// Get the data from the request body and decode the JSON into a MessageWrapper
	defer r.Body.Close()
	var mw data.Wrapper
	if err := json.NewDecoder(r.Body).Decode(&mw); err != nil {
		mr := MessageResponse{
			Received:    false,
			ErrorString: "Unable to decode JSON : " + err.Error(),
		}
		if err := json.NewEncoder(w).Encode(&mr); err != nil {
			log.Printf("Unable to send response back to client : %v\n", err)
		}
		return
	}

	// If the message doesn't already have an id, generate one.
	if mw.Message.Uuid == "" {
		u := uuid.NewRandom()
		mw.Message.Uuid = u.String()
	}

	// Send a response back to the client to confirm receipt of the message
	mr := MessageResponse{
		Received:  true,
		MessageId: mw.Message.Uuid,
	}
	if err := json.NewEncoder(w).Encode(&mr); err != nil {
		log.Printf("Unable to send response back to client : %v\n", err)
		return
	}

	// Check to see whether a queue has been specified, if not, set it to default.
	if mw.QueueID == "" {
		mw.QueueID = "default"
	}

	// Queue the message for delivery.
	context.InFlight <- &mw
}