Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/ibrt/golang-inject-clock

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/ibrt/golang-inject-clock

  • v1.2.2
  • Source
  • Go
  • Socket score

Version published
Created
Source

golang-inject-clock

Go Reference CI codecov

Clock module for the golang-inject framework.

Basic Usage

This module injects a "clock" into Go context. It provides both a real and a mock implementation for use in tests. Beyond being useful by itself, it is also a minimal example of how to tie together modules using the golang-inject framework, and how to easily test implementations using the golang-fixtures test suites.

// main.go

package main

import (
    "net/http"
    "time"

    "github.com/ibrt/golang-inject/injectz"
    "github.com/ibrt/golang-inject-clock/clockz"
)

func main() {
    injector, releaser := injectz.Initialize(clockz.Initializer)
    defer releaser()

    middleware := injectz.NewMiddleware(injector)
    mux := http.NewServeMux()
    mux.Handle("/", middleware(http.HandlerFunc(handler)))
    _ = http.ListenAndServe(":3000", mux)
}

func handler(w http.ResponseWriter, r *http.Request) {
    _, _ = w.Write([]byte(clockz.Get(r.Context()).Now().Format(time.RFC3339Nano)))
}
// main_test.go

package main

import (
    "context"
    "net/http"
    "net/http/httptest"
    "testing"
    "time"

    "github.com/ibrt/golang-fixtures/fixturez"
    "github.com/ibrt/golang-inject-clock/clockz/testclockz"
    "github.com/stretchr/testify/require"
)

var (
    _ fixturez.Suite = &Suite{}
)

type Suite struct {
    *fixturez.DefaultConfigMixin
    Clock *testclockz.MockHelper
}

func TestSuite(t *testing.T) {
    fixturez.RunSuite(t, &Suite{})
}

func (s *Suite) TestHandler(ctx context.Context, t *testing.T) {
    const nowStr = "2009-11-18T08:04:34.829482969Z"
    now, err := time.Parse(time.RFC3339Nano, nowStr)
    require.NoError(t, err)
    s.Clock.Mock.Set(now)

    w := httptest.NewRecorder()
    r := httptest.NewRequest("GET", "/", nil).WithContext(ctx)

    handler(w, r)

    require.Equal(t, http.StatusOK, w.Code)
    require.Equal(t, nowStr, w.Body.String())
}

Developers

Contributions are welcome, please check in on proposed implementation before sending a PR. You can validate your changes using the ./test.sh script.

FAQs

Package last updated on 23 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc