Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 25, 2023
1 parent 9482770 commit c3c6366
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ docker run -p 80:3000 evg4b/uncors --from 'http://local.github.com' --to 'https:

## Stew (Cross-platform)

Also, you can install binaris using [Stew](https://github.com/marwanhawari/) with the following commands:
Also, you can install binaries using [Stew](https://github.com/marwanhawari/) with the following commands:

```bash
stew install evg4b/uncors
Expand Down Expand Up @@ -139,7 +139,7 @@ The following command can be used to start the UNCORS proxy server:
uncors --from 'http://localhost' --to 'https://github.com' --http-port 8080
```

More information about configuration and usage you can fiund on [UNCORS wiki](https://github.com/evg4b/uncors/wiki).
More information about configuration and usage you can find on [UNCORS wiki](https://github.com/evg4b/uncors/wiki).

# ⚠️ Caution

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
)

require (
atomicgo.dev/cursor v0.1.1 // indirect
atomicgo.dev/cursor v0.1.2 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.0.2 // indirect
github.com/containerd/console v1.0.3 // indirect
Expand All @@ -45,7 +45,7 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
Expand Down
159 changes: 155 additions & 4 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
fmt "fmt"
"fmt"

"github.com/evg4b/uncors/internal/config/hooks"
"github.com/mitchellh/mapstructure"
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/mock/serve_file_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (m *Middleware) serveFileContent(writer http.ResponseWriter, request *http.
fileName := m.response.File
file, err := m.fs.OpenFile(fileName, os.O_RDONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("filed to opent file %s: %w", fileName, err)
return fmt.Errorf("filed to open file %s: %w", fileName, err)
}

stat, err := file.Stat()
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/static/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (m *Middleware) openIndexFile() (afero.File, error) {

file, err := m.fs.Open(m.index)
if err != nil {
return nil, fmt.Errorf("filed to opend index file: %w", err)
return nil, fmt.Errorf("filed to open index file: %w", err)
}

return file, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/static/response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (
)

func TestResponseWriterWrapper(t *testing.T) {
const expectedValye = `{ "status": "ok" }`
const expectedValue = `{ "status": "ok" }`
const expectedCode = 201

recorder := httptest.NewRecorder()
writer := static.WrapResponseWriter(recorder)

writer.WriteHeader(expectedCode)
sfmt.Fprint(writer, expectedValye)
sfmt.Fprint(writer, expectedValue)

t.Run("save status code", func(t *testing.T) {
assert.Equal(t, expectedCode, writer.StatusCode)
})

t.Run("write body", func(t *testing.T) {
assert.Equal(t, expectedValye, testutils.ReadBody(t, recorder))
assert.Equal(t, expectedValue, testutils.ReadBody(t, recorder))
})
}
4 changes: 2 additions & 2 deletions internal/handler/uncors_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestUncorsRequestHandler(t *testing.T) {
hand.ServeHTTP(recorder, request)

assert.Equal(t, http.StatusInternalServerError, recorder.Code)
expectedMessage := "filed to opend index file: open /assets/index.php: file does not exist"
expectedMessage := "filed to open index file: open /assets/index.php: file does not exist"
assert.Contains(t, testutils.ReadBody(t, recorder), expectedMessage)
})
})
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestUncorsRequestHandler(t *testing.T) {
hand.ServeHTTP(recorder, request)

assert.Equal(t, http.StatusInternalServerError, recorder.Code)
expectedMessage := "filed to opent file /unknown.json: open /unknown.json: file does not exist"
expectedMessage := "filed to open file /unknown.json: open /unknown.json: file does not exist"
assert.Contains(t, testutils.ReadBody(t, recorder), expectedMessage)
})
})
Expand Down
14 changes: 7 additions & 7 deletions internal/urlreplacer/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (

var (
ErrInvalidSourceURL = errors.New("source url is invalid")
ErrURLNotMached = errors.New("is not matched")
ErrURLNotMatched = errors.New("is not matched")
)

type hook = func(string) string
Expand Down Expand Up @@ -69,17 +69,17 @@ func NewReplacer(source, target string) (*Replacer, error) {
func (r *Replacer) Replace(source string) (string, error) {
matches := r.regexp.FindStringSubmatch(source)
if len(matches) < 1 {
return "", fmt.Errorf("url '%s' %w", source, ErrURLNotMached)
return "", fmt.Errorf("url '%s' %w", source, ErrURLNotMatched)
}

replaced := strings.Clone(r.pattern)

for _, subexpName := range r.regexp.SubexpNames() {
if len(subexpName) > 0 {
partPattern := sfmt.Sprintf("${%s}", subexpName)
partIndex := r.regexp.SubexpIndex(subexpName)
for _, subExpName := range r.regexp.SubexpNames() {
if len(subExpName) > 0 {
partPattern := sfmt.Sprintf("${%s}", subExpName)
partIndex := r.regexp.SubexpIndex(subExpName)
partValue := matches[partIndex]
if hook, ok := r.hooks[subexpName]; ok {
if hook, ok := r.hooks[subExpName]; ok {
partValue = hook(partValue)
}

Expand Down

0 comments on commit c3c6366

Please sign in to comment.