summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--svc/handlers.go6
-rw-r--r--svc/handlers_test.go71
2 files changed, 68 insertions, 9 deletions
diff --git a/svc/handlers.go b/svc/handlers.go
index 5bb0d4f..02975ef 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -133,9 +133,13 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 		out, err = twtxtCache.QueryInStatus("@<")
 		out = registry.ReduceToPage(page, out)
 
-	default:
+	case "/api/plain/tweets":
 		out, err = twtxtCache.QueryAllStatuses()
 		out = registry.ReduceToPage(page, out)
+
+	default:
+		log404(w, r, fmt.Errorf("endpoint not found"))
+		return
 	}
 	errLog("", err)
 
diff --git a/svc/handlers_test.go b/svc/handlers_test.go
index 36c795a..fa73de5 100644
--- a/svc/handlers_test.go
+++ b/svc/handlers_test.go
@@ -52,17 +52,72 @@ func Test_apiFormatHandler(t *testing.T) {
 		}
 	})
 }
+
+var endpointCases = []struct {
+	name   string
+	req    *http.Request
+	status int
+}{
+	{
+		name:   "Regular Query: /api/plain/users",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/users", nil),
+		status: http.StatusOK,
+	},
+	{
+		name:   "Regular Query: /api/plain/mentions",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/mentions", nil),
+		status: http.StatusOK,
+	},
+	{
+		name:   "Regular Query: /api/plain/tweets",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tweets", nil),
+		status: http.StatusOK,
+	},
+	{
+		name:   "Invalid Endpoint: /api/plain/statuses",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/statuses", nil),
+		status: http.StatusNotFound,
+	},
+}
+
 func Test_apiEndpointHandler(t *testing.T) {
 	initTestConf()
-	t.Run("apiEndpointHandler", func(t *testing.T) {
-		w := httptest.NewRecorder()
-		req := httptest.NewRequest("GET", "localhost"+testport+"/api/plain/users", nil)
-		apiEndpointHandler(w, req)
-		resp := w.Result()
-		if resp.StatusCode != http.StatusOK {
-			t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
+	mockRegistry()
+	for _, tt := range endpointCases {
+		t.Run(tt.name, func(t *testing.T) {
+			w := httptest.NewRecorder()
+			apiEndpointHandler(w, tt.req)
+			resp := w.Result()
+			if resp.StatusCode != tt.status {
+				t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
+			}
+			if tt.status == http.StatusOK {
+				var body []byte
+				buf := bytes.NewBuffer(body)
+				err := resp.Write(buf)
+				if err != nil {
+					t.Errorf("%v\n", err)
+				}
+				if buf == nil {
+					t.Errorf("Got nil\n")
+				}
+				if len(buf.Bytes()) == 0 {
+					t.Errorf("Got zero data\n")
+				}
+			}
+		})
+	}
+}
+func Benchmark_apiEndpointHandler(b *testing.B) {
+	initTestConf()
+	mockRegistry()
+	w := httptest.NewRecorder()
+	b.ResetTimer()
+	for _, tt := range endpointCases {
+		for i := 0; i < b.N; i++ {
+			apiEndpointHandler(w, tt.req)
 		}
-	})
+	}
 }
 
 func Test_apiTagsBaseHandler(t *testing.T) {
lor: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include <glib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>

#include <stabber.h>
#include <expect.h>

#include "proftest.h"

void
sends_new_item(void **state)
{
    prof_connect();

    stbbr_for_query("jabber:iq:roster",
        "<iq type='set' from='stabber@localhost'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='bob@localhost' subscription='none' name=''/>"
            "</query>"
        "</iq>"
    );

    prof_input("/roster add bob@localhost");

    assert_true(stbbr_received(
        "<iq type='set' id='*'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='bob@localhost' name=''/>"
            "</query>"
        "</iq>"
    ));

    assert_true(prof_output_exact("Roster item added: bob@localhost"));
}

void
sends_new_item_nick(void **state)
{
    prof_connect();

    stbbr_for_query("jabber:iq:roster",
        "<iq type='set' from='stabber@localhost'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='bob@localhost' subscription='none' name='Bobby'/>"
            "</query>"
        "</iq>"
    );

    prof_input("/roster add bob@localhost Bobby");

    assert_true(stbbr_received(
        "<iq type='set' id='*'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='bob@localhost' name='Bobby'/>"
            "</query>"
        "</iq>"
    ));

    assert_true(prof_output_exact("Roster item added: bob@localhost (Bobby)"));
}

void
sends_remove_item(void **state)
{
    prof_connect_with_roster(
        "<item jid='buddy1@localhost' subscription='both'/>"
        "<item jid='buddy2@localhost' subscription='both'/>"
    );

    stbbr_for_query("jabber:iq:roster",
        "<iq id='*' type='set'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='buddy1@localhost' subscription='remove'/>"
            "</query>"
        "</iq>"
    );

    prof_input("/roster remove buddy1@localhost");

    assert_true(stbbr_received(
        "<iq type='set' id='*'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='buddy1@localhost' subscription='remove'/>"
            "</query>"
        "</iq>"
    ));

    assert_true(prof_output_exact("Roster item removed: buddy1@localhost"));
}

void
sends_nick_change(void **state)
{
    prof_connect_with_roster(
        "<item jid='buddy1@localhost' subscription='both'/>"
    );

    prof_input("/roster nick buddy1@localhost Buddy1");

    assert_true(prof_output_exact("Nickname for buddy1@localhost set to: Buddy1."));

    assert_true(stbbr_received(
        "<iq type='set' id='*'>"
            "<query xmlns='jabber:iq:roster'>"
                "<item jid='buddy1@localhost' name='Buddy1'/>"
            "</query>"
        "</iq>"
    ));
}