Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 25, 2023
1 parent b6bb5c0 commit cb06878
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 118 deletions.
10 changes: 5 additions & 5 deletions .run/format.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="format" type="CompoundRunConfigurationType">
<toRun name="gofmt" type="ShConfigurationType" />
<toRun name="gofumpt" type="ShConfigurationType" />
<method v="2" />
</configuration>
<configuration default="false" name="format" type="CompoundRunConfigurationType">
<toRun name="gofmt" type="ShConfigurationType"/>
<toRun name="gofumpt" type="ShConfigurationType"/>
<method v="2"/>
</configuration>
</component>
21 changes: 11 additions & 10 deletions .run/go test uncors.run.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="go test uncors" type="GoTestRunConfiguration" factoryName="Go Test" nameIsGenerated="true">
<module name="uncors" />
<working_directory value="$PROJECT_DIR$" />
<kind value="DIRECTORY" />
<package value="github.com/evg4b/uncors" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<method v="2" />
</configuration>
<configuration default="false" name="go test uncors" type="GoTestRunConfiguration" factoryName="Go Test"
nameIsGenerated="true">
<module name="uncors"/>
<working_directory value="$PROJECT_DIR$"/>
<kind value="DIRECTORY"/>
<package value="github.com/evg4b/uncors"/>
<directory value="$PROJECT_DIR$"/>
<filePath value="$PROJECT_DIR$"/>
<framework value="gotest"/>
<method v="2"/>
</configuration>
</component>
30 changes: 15 additions & 15 deletions .run/gofmt.run.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="gofmt" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="gofmt -l -s -w ." />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/zsh" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
<configuration default="false" name="gofmt" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="gofmt -l -s -w ."/>
<option name="INDEPENDENT_SCRIPT_PATH" value="true"/>
<option name="SCRIPT_PATH" value=""/>
<option name="SCRIPT_OPTIONS" value=""/>
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true"/>
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$"/>
<option name="INDEPENDENT_INTERPRETER_PATH" value="true"/>
<option name="INTERPRETER_PATH" value="/bin/zsh"/>
<option name="INTERPRETER_OPTIONS" value=""/>
<option name="EXECUTE_IN_TERMINAL" value="true"/>
<option name="EXECUTE_SCRIPT_FILE" value="false"/>
<envs/>
<method v="2"/>
</configuration>
</component>
8 changes: 4 additions & 4 deletions internal/config/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ func (m Mappings) String() string {

for _, group := range lo.GroupBy(m, extractHost) {
for _, mapping := range group {
helpers.Fprintf(builder, "%s => %s\n", mapping.From, mapping.To)
helpers.FPrintf(builder, "%s => %s\n", mapping.From, mapping.To)
}

mapping := group[0]
for _, mock := range mapping.Mocks {
helpers.Fprintf(builder, " mock: %s\n", mock.String())
helpers.FPrintf(builder, " mock: %s\n", mock.String())
}
for _, static := range mapping.Statics {
helpers.Fprintf(builder, " static: %s\n", static.String())
helpers.FPrintf(builder, " static: %s\n", static.String())
}
for _, cacheGlob := range mapping.Cache {
helpers.Fprintf(builder, " cache: %s\n", cacheGlob)
helpers.FPrintf(builder, " cache: %s\n", cacheGlob)
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/config/validators/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestMappingValidator(t *testing.T) {
},
},
Cache: config.CacheGlobs{
"/api/info",
"/api/constants",
"/**",
},
},
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestMappingValidator(t *testing.T) {
Statics: []config.StaticDirectory{},
Mocks: []config.Mock{
{
Path: "/api/info",
Path: "/api/user",
Method: "invalid",
Response: config.Response{
Code: 200,
Expand Down
4 changes: 2 additions & 2 deletions internal/contracts/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestCastToHTTPHandler(t *testing.T) {
const expectedBody = `{ "OK": true }`
handlerStub := contracts.HandlerFunc(func(w contracts.ResponseWriter, r *contracts.Request) {
w.WriteHeader(http.StatusOK)
helpers.Fprint(w, expectedBody)
helpers.FPrint(w, expectedBody)
})

request := httptest.NewRequest(http.MethodGet, "/data", nil)
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestHandlerFunc(t *testing.T) {
const expectedBody = `{ "OK": true }`
uncorsHandler := contracts.HandlerFunc(func(w contracts.ResponseWriter, r *contracts.Request) {
w.WriteHeader(http.StatusOK)
helpers.Fprint(w, expectedBody)
helpers.FPrint(w, expectedBody)
})

recorder := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion internal/contracts/response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestResponseWriterWrapper(t *testing.T) {
writer := contracts.WrapResponseWriter(recorder)

writer.WriteHeader(expectedCode)
helpers.Fprint(writer, expectedValue)
helpers.FPrint(writer, expectedValue)

t.Run("save status code", func(t *testing.T) {
assert.Equal(t, expectedCode, writer.StatusCode())
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/cache/cacheable_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestCacheableResponseWriter(t *testing.T) {
{
name: "write body bytes only",
action: func(w http.ResponseWriter) {
helpers.Fprint(w, bodyString)
helpers.FPrint(w, bodyString)
},
expected: &cache.CachedResponse{
Header: http.Header{
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestCacheableResponseWriter(t *testing.T) {
action: func(w http.ResponseWriter) {
header := w.Header()
header.Set(headers.ContentLength, "999")
helpers.Fprint(w, bodyString)
helpers.FPrint(w, bodyString)
},
expected: &cache.CachedResponse{
Header: http.Header{
Expand All @@ -81,7 +81,7 @@ func TestCacheableResponseWriter(t *testing.T) {
header.Set(headers.ContentLength, "9")
header.Set(headers.Authorization, authorization)
writer.WriteHeader(http.StatusBadGateway)
helpers.Fprint(writer, bodyString)
helpers.FPrint(writer, bodyString)
},
expected: &cache.CachedResponse{
Code: http.StatusBadGateway,
Expand Down
20 changes: 11 additions & 9 deletions internal/handler/cache/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

func TestCacheMiddleware(t *testing.T) {
const expectedBody = "this is test"
const cacheGlob = "/api/**"
const constantEndpoint = "/api/constants"

expectedHeader := http.Header{
headers.ContentType: {"text/html; charset=iso-8859-1"},
Expand All @@ -32,14 +34,14 @@ func TestCacheMiddleware(t *testing.T) {
cache.WithMethods([]string{http.MethodGet}),
cache.WithGlobs(config.CacheGlobs{
"/translations",
"/api/**",
cacheGlob,
}),
)

handler := testutils.NewCounter(func(writer contracts.ResponseWriter, request *contracts.Request) {
writer.WriteHeader(http.StatusOK)
testutils.CopyHeaders(expectedHeader, writer.Header())
helpers.Fprintf(writer, expectedBody)
helpers.FPrintf(writer, expectedBody)
})

t.Run("should not call cached response just one time for", func(t *testing.T) {
Expand Down Expand Up @@ -135,19 +137,19 @@ func TestCacheMiddleware(t *testing.T) {
{
name: "witch response with status code 500",
method: http.MethodGet,
path: "/api/constants",
path: constantEndpoint,
statusCode: http.StatusInternalServerError,
},
{
name: "witch response with status code 400",
method: http.MethodGet,
path: "/api/constants",
path: constantEndpoint,
statusCode: http.StatusBadRequest,
},
{
name: "witch response with status code 304",
method: http.MethodGet,
path: "/api/constants",
path: constantEndpoint,
statusCode: http.StatusNotModified,
},
}
Expand All @@ -156,7 +158,7 @@ func TestCacheMiddleware(t *testing.T) {
handler := testutils.NewCounter(func(writer contracts.ResponseWriter, request *contracts.Request) {
writer.WriteHeader(testCase.statusCode)
testutils.CopyHeaders(expectedHeader, writer.Header())
helpers.Fprintf(writer, expectedBody)
helpers.FPrintf(writer, expectedBody)
})

wrappedHandler := middleware.Wrap(handler)
Expand Down Expand Up @@ -184,7 +186,7 @@ func TestCacheMiddleware(t *testing.T) {
cache.WithCacheStorage(goCache.New(time.Minute, time.Minute)),
cache.WithLogger(mocks.NewNoopLogger(t)),
cache.WithMethods([]string{http.MethodGet}),
cache.WithGlobs(config.CacheGlobs{"/api/**"}),
cache.WithGlobs(config.CacheGlobs{cacheGlob}),
)

wrappedHandler := middleware.Wrap(handler)
Expand Down Expand Up @@ -212,13 +214,13 @@ func TestCacheMiddleware(t *testing.T) {
cache.WithCacheStorage(goCache.New(time.Minute, time.Minute)),
cache.WithLogger(mocks.NewNoopLogger(t)),
cache.WithMethods(methods),
cache.WithGlobs(config.CacheGlobs{"/api/**"}),
cache.WithGlobs(config.CacheGlobs{cacheGlob}),
)

handler := testutils.NewCounter(func(writer contracts.ResponseWriter, request *contracts.Request) {
writer.WriteHeader(http.StatusOK)
testutils.CopyHeaders(expectedHeader, writer.Header())
helpers.Fprintf(writer, request.Method)
helpers.FPrintf(writer, request.Method)
})

wrappedHandler := middleware.Wrap(handler)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/mock/serve_raw_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func (h *Handler) serveRawContent(writer http.ResponseWriter) {
}

writer.WriteHeader(normaliseCode(response.Code))
helpers.Fprint(writer, response.Raw)
helpers.FPrint(writer, response.Raw)
}
2 changes: 1 addition & 1 deletion internal/handler/static/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestStaticMiddleware(t *testing.T) {

handler := middleware.Wrap(contracts.HandlerFunc(func(writer contracts.ResponseWriter, _ *contracts.Request) {
writer.WriteHeader(testHTTPStatusCode)
helpers.Fprint(writer, testHTTPBody)
helpers.FPrint(writer, testHTTPBody)
}))

t.Run("return static content", func(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions internal/helpers/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/stretchr/testify/assert"
)

type clonableTestStruct struct{ Value string }
type cloneableTestStruct struct{ Value string }

func (t *clonableTestStruct) Clone() clonableTestStruct {
return clonableTestStruct{Value: "Cloned:" + t.Value}
func (t *cloneableTestStruct) Clone() cloneableTestStruct {
return cloneableTestStruct{Value: "Cloned:" + t.Value}
}

type nonClonableTestStruct struct{ Value string }
type nonCloneableTestStruct struct{ Value string }

func TestCloneMap(t *testing.T) {
t.Run("base types", func(t *testing.T) {
Expand Down Expand Up @@ -73,14 +73,14 @@ func TestCloneMap(t *testing.T) {
})
})

t.Run("clonable objects", func(t *testing.T) {
data := map[string]clonableTestStruct{
t.Run("cloneable objects", func(t *testing.T) {
data := map[string]cloneableTestStruct{
"1": {Value: "property 1"},
"2": {Value: "property 2"},
"3": {Value: "property 3"},
}

expected := map[string]clonableTestStruct{
expected := map[string]cloneableTestStruct{
"1": {Value: "Cloned:property 1"},
"2": {Value: "Cloned:property 2"},
"3": {Value: "Cloned:property 3"},
Expand All @@ -92,8 +92,8 @@ func TestCloneMap(t *testing.T) {
assert.EqualValues(t, &expected, &actual)
})

t.Run("non clonable objects", func(t *testing.T) {
data := map[string]nonClonableTestStruct{
t.Run("non cloneable objects", func(t *testing.T) {
data := map[string]nonCloneableTestStruct{
"1": {Value: "demo"},
"2": {Value: "demo"},
"3": {Value: "demo"},
Expand Down
6 changes: 3 additions & 3 deletions internal/helpers/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"io"
)

func Fprint(w io.Writer, payload ...any) {
func FPrint(w io.Writer, payload ...any) {
if _, err := fmt.Fprint(w, payload...); err != nil {
panic(err)
}
}

func Fprintf(w io.Writer, format string, a ...any) {
func FPrintf(w io.Writer, format string, a ...any) {
if _, err := fmt.Fprintf(w, format, a...); err != nil {
panic(err)
}
Expand All @@ -21,7 +21,7 @@ func Sprintf(format string, a ...any) string {
return fmt.Sprintf(format, a...)
}

func Fprintln(w io.Writer, a ...any) {
func FPrintln(w io.Writer, a ...any) {
if _, err := fmt.Fprintln(w, a...); err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit cb06878

Please sign in to comment.