func TestCustomPrefix(t *testing.T) {
	Convey("Given the App running with PREFIX", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		f := Flag
		f.Prefix = "BETA"
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", f)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting a message", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte(""))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__prefix\":\"BETA\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

	})

	Convey("Given the App running WITHOUT prefix", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		f := Flag
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", f)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting a message", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte(""))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldNotContainSubstring, "\"__prefix\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

	})
}
Пример #2
0
// WithValue returns a copy of parent in which the value associated with key is val.
func WithValue(parent Context, key interface{}, val interface{}) Context {
	internalCtx, ok := parent.(context.Context)
	if !ok {
		panic(stderrs.New("Invalid context type"))
	}
	return context.WithValue(internalCtx, key, val)
}
func TestPostMany(t *testing.T) {
	Convey("Given the App running", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		f := Flag
		f.BufferSize = 100
		f.BufferTimeout = "100ms"
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", f)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting BufferSize + 10 messages", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			for i := 1; i <= f.BufferSize+10; i++ {
				post_body := bytes.NewReader([]byte("{\"i\":\"" + strconv.Itoa(i) + "\"}"))
				http.Post(ts.URL+"/log/many/at/once/", "application/json", post_body)
			}

			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__level\":\"many\"")
			So(body, ShouldContainSubstring, "\"__category\":\"at\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"once\"")
			for i := 1; i <= f.BufferSize; i++ {
				So(body, ShouldContainSubstring, "\"i\":\""+strconv.Itoa(i)+"\"")
			}
			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body = <-bc
			for i := f.BufferSize + 1; i <= f.BufferSize+10; i++ {
				So(body, ShouldContainSubstring, "\"i\":\""+strconv.Itoa(i)+"\"")
			}

			body = "EMPTY"
			var uc_value string = "EMPTY"
			select {
			case uc_value = <-uc:
			case <-time.After(time.Second):
			}
			select {
			case body = <-bc:
			case <-time.After(time.Second):
			}
			So(uc_value, ShouldEqual, "EMPTY")
			So(body, ShouldEqual, "EMPTY")
		})

	})
}
func TestHeaderVariables(t *testing.T) {
	Convey("Given the App running", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		ctx, _ := context.WithCancel(context.Background())
		f := Flag
		ctx = context.WithValue(ctx, "Flag", f)

		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting __namespace in the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()

			post_body := bytes.NewReader([]byte(""))

			client := &http.Client{}
			req, err := http.NewRequest("POST", ts.URL+"/log/fakelevel/fakecategory/fakeslug/", post_body)
			So(err, ShouldBeNil)
			req.Header.Add("__namespace", "some_custom_namespace")
			req.Header.Add("__date", "some_custom_date")
			req.Header.Add("Content-type", "application/json")
			resp, err := client.Do(req)
			defer resp.Body.Close()

			<-uc
			body := <-bc
			So(body, ShouldContainSubstring, "\"__namespace\":\"some_custom_namespace\"")
			So(body, ShouldContainSubstring, "\"__date\":\"some_custom_date\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

	})
}
Пример #5
0
// NewContext returns a new Context carrying userIP.
func NewContext(ctx context.Context, userIP net.IP) context.Context {
	return context.WithValue(ctx, &key, userIP)
}
func TestPostToLogApi(t *testing.T) {
	Convey("Given the App running", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", Flag)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))

		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting empty body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte(""))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__date\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

		Convey("When posting {} as the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("{}"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__date\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

		Convey("When posting __date in the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("{\"__date\":\"123\"}"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__date\":\"123\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

		Convey("When posting __namespace in the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("{\"__namespace\":\"my_namespace\"}"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__namespace\":\"my_namespace\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

		Convey("When posting empty __namespace in the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("{\"__namespace\":\"\"}"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			//should use the hostname as the namespace
			So(body, ShouldNotContainSubstring, "\"__namespace\":\"\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

		Convey("When posting custom keys in the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("{\"name\":\"John Doe\",\"date\":\"123\"}"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			//should use the hostname as the namespace
			So(body, ShouldNotContainSubstring, "\"__date\":\"123\"")
			So(body, ShouldContainSubstring, "\"__date\"")
			So(body, ShouldContainSubstring, "\"date\":\"123\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
			So(body, ShouldContainSubstring, "\"name\":\"John Doe\"")
			So(body, ShouldContainSubstring, "\"date\":\"123\"")
		})

		Convey("When posting custom keys in the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("{\"name\":\"John Doe\",\"date\":\"123\"}"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+Flag.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			//should use the hostname as the namespace
			So(body, ShouldNotContainSubstring, "\"__date\":\"123\"")
			So(body, ShouldContainSubstring, "\"__date\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
			So(body, ShouldContainSubstring, "\"name\":\"John Doe\"")
			So(body, ShouldContainSubstring, "\"date\":\"123\"")
		})

		Convey("When posting invalid json as the body", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte("THIS IS AN INVALID JSON"))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			var uc_value string = "EMPTY"
			select {
			case uc_value = <-uc:
			case <-time.After(time.Second):
			}
			var body string = "EMPTY"
			select {
			case body = <-bc:
			case <-time.After(time.Second):
			}
			So(uc_value, ShouldEqual, "EMPTY")
			So(body, ShouldEqual, "EMPTY")
		})
	})
}
Пример #7
0
func (ctx *AuthContext) saveId(id string) {
	ctx.Context = context.WithValue(ctx.Context, userIdKey, id)
}
Пример #8
0
func (ctx *AuthContext) saveToken(token string) {
	ctx.Context = context.WithValue(ctx.Context, userTokenKey, token)
}
Пример #9
0
func NewContext(ctx context.Context, reqID RequestID) context.Context {
	return context.WithValue(ctx, reqIDKey, reqID)
}
Пример #10
0
func ContextualOne(ctx context.Context, rw http.ResponseWriter, r *http.Request, next Handler) {
	next.ServeHTTP(context.WithValue(ctx, key, "hello"), rw, r)
}
Пример #11
0
// NewServiceNameContext returns a new context carrying a service name
func NewServiceNameContext(ctx context.Context, serviceName string) context.Context {
	return context.WithValue(ctx, serviceNameKey, serviceName)
}
Пример #12
0
func NewContext(ctx context.Context, l *log.Entry) context.Context {
	return context.WithValue(ctx, loggerKey, l)
}
Пример #13
0
func NewContextFromConfig(c ConfigStruct) context.Context {
	ctx, _ := context.WithCancel(context.Background())
	ctx = context.WithValue(ctx, "Config", c)
	return ctx
}
Пример #14
0
func newContext(parent context.Context, s *serverContext) context.Context {
	return context.WithValue(parent, "serverContext", s)
}
Пример #15
0
func dbmContext(dbm *gorp.DbMap) context.Context {
	ctx := context.Background()
	return context.WithValue(ctx, "dbm", dbm)
}