Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webservice
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
masi9606
webservice
Commits
625723d7
Commit
625723d7
authored
1 year ago
by
gjahn
Browse files
Options
Downloads
Patches
Plain Diff
Add HEAD method to obtain MIME type & size w/o actually fetching the data
parent
d3ef56c4
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+7
-0
7 additions, 0 deletions
README.md
routing/routes.go
+17
-0
17 additions, 0 deletions
routing/routes.go
routing/routes_test.go
+11
-1
11 additions, 1 deletion
routing/routes_test.go
with
35 additions
and
1 deletion
README.md
+
7
−
0
View file @
625723d7
...
...
@@ -86,6 +86,13 @@ curl \
http://localhost:8080/state/bar
```
Find out MIME type and size of an entry:
```
bash
curl
\
-X
HEAD
\
http://localhost:8080/state/bar
```
Obtain an entry:
```
bash
curl
\
...
...
This diff is collapsed.
Click to expand it.
routing/routes.go
+
17
−
0
View file @
625723d7
...
...
@@ -194,6 +194,23 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
})
statePathGroup
.
Head
(
"/:name"
,
func
(
c
*
f
.
Ctx
)
error
{
name
:=
strings
.
Clone
(
c
.
Params
(
"name"
)
)
existingItem
,
err
:=
store
.
Fetch
(
name
)
if
err
!=
nil
{
return
c
.
SendStatus
(
http
.
StatusInternalServerError
)
}
if
existingItem
==
nil
{
return
c
.
SendStatus
(
http
.
StatusNotFound
)
}
c
.
Set
(
"Content-Type"
,
existingItem
.
MimeType
()
)
c
.
Set
(
"Content-Length"
,
fmt
.
Sprintf
(
"%d"
,
len
(
existingItem
.
Data
()
)
)
)
return
c
.
SendStatus
(
http
.
StatusOK
)
})
statePathGroup
.
Use
(
"*"
,
func
(
c
*
f
.
Ctx
)
error
{
if
method
:=
c
.
Method
();
method
==
"OPTIONS"
{
c
.
Set
(
"Allow"
,
"GET, PUT, DELETE, OPTIONS"
)
...
...
This diff is collapsed.
Click to expand it.
routing/routes_test.go
+
11
−
1
View file @
625723d7
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"math/rand"
...
...
@@ -167,7 +168,7 @@ func TestState( t *testing.T ){
const
statePath2
=
"/state/another-test"
const
statePath2Mime
=
"application/octet-stream"
const
statePath2BodySize
=
64
const
statePath2BodySize
=
128
statePath2Body
:=
generateRandomBytes
(
statePath2BodySize
)
req
:=
ht
.
NewRequest
(
"GET"
,
statePath1
,
nil
)
...
...
@@ -211,6 +212,15 @@ func TestState( t *testing.T ){
res
,
_
=
router
.
Test
(
req
,
-
1
)
assert
.
Equal
(
t
,
http
.
StatusCreated
,
res
.
StatusCode
)
req
=
ht
.
NewRequest
(
"HEAD"
,
statePath2
,
nil
)
res
,
_
=
router
.
Test
(
req
,
-
1
)
contentLength
,
err
:=
strconv
.
ParseInt
(
res
.
Header
[
"Content-Length"
][
0
],
10
,
64
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
assert
.
Equal
(
t
,
statePath2Mime
,
res
.
Header
[
"Content-Type"
][
0
]
)
assert
.
Equal
(
t
,
int64
(
statePath2BodySize
),
contentLength
)
assert
.
IsType
(
t
,
res
.
Body
,
http
.
NoBody
)
req
=
ht
.
NewRequest
(
"GET"
,
statePath2
,
nil
)
res
,
_
=
router
.
Test
(
req
,
-
1
)
bodyBytes
,
err
:=
io
.
ReadAll
(
res
.
Body
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment