Skip to content

Commit

Permalink
Merge pull request #16 from cheqd/dev-1530-http-header
Browse files Browse the repository at this point in the history
fix: Fix headers contentType behavior
  • Loading branch information
Toktar authored Jul 13, 2022
2 parents 71a3c5b + 07fc8b7 commit 0d98362
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
8 changes: 4 additions & 4 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func serve() {
accept := strings.Split(c.Request().Header.Get(echo.HeaderAccept), ";")[0]
log.Trace().Msgf("Accept: %s", accept)

var requestedContentType types.ContentType
if strings.Contains(accept, string(types.JSONLD)) {
requestedContentType := types.ContentType(accept)
if accept == "*/*" {
requestedContentType = types.DIDJSONLD
} else {
requestedContentType = types.DIDJSON
} else if strings.Contains(accept, string(types.HTML)) {
requestedContentType = types.HTML
}
log.Debug().Msgf("Requested content type: %s", requestedContentType)

Expand Down
21 changes: 18 additions & 3 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
// jsonpb Marshaller is deprecated, but is needed because there's only one way to proto
// marshal in combination with our proto generator version
"encoding/json"
"fmt"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -31,13 +32,22 @@ func (rs RequestService) IsDidUrl(didUrl string) bool {
}

func (rs RequestService) ProcessDIDRequest(didUrl string, resolutionOptions types.ResolutionOption) (string, error) {
var result string
var err error
if rs.IsDidUrl(didUrl) {
log.Trace().Msgf("Dereferencing %s", didUrl)
return rs.prepareDereferencingResult(didUrl, types.DereferencingOption(resolutionOptions))
result, err = rs.prepareDereferencingResult(didUrl, types.DereferencingOption(resolutionOptions))
} else {
log.Trace().Msgf("Resolving %s", didUrl)
return rs.prepareResolutionResult(didUrl, resolutionOptions)
result, err = rs.prepareResolutionResult(didUrl, resolutionOptions)
}

if resolutionOptions.Accept == types.HTML {
return "<!DOCTYPE html><html><body><h1>Cheqd DID Resolver</h1><pre id=\"r\"></pre><script> var data = " +
result + ";document.getElementById(\"r\").innerHTML = JSON.stringify(data, null, 4);" +
"</script></body></html>", err
}
return result, err
}

func (rs RequestService) prepareResolutionResult(did string, resolutionOptions types.ResolutionOption) (string, error) {
Expand Down Expand Up @@ -123,9 +133,14 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
}

if didResolutionMetadata.ContentType == types.DIDJSONLD {
if didResolutionMetadata.ContentType == types.DIDJSONLD || didResolutionMetadata.ContentType == types.JSONLD {
didDoc.Context = append(didDoc.Context, types.DIDSchemaJSONLD)
} else if didResolutionMetadata.ContentType == types.DIDJSON || didResolutionMetadata.ContentType == types.HTML {
didDoc.Context = []string{}
} else {
return types.DidResolution{}, fmt.Errorf("content type %s is not supported", didResolutionMetadata.ContentType)
}

return types.DidResolution{Did: didDoc, Metadata: metadata, ResolutionMetadata: didResolutionMetadata}, nil
}

Expand Down
4 changes: 3 additions & 1 deletion services/request_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ func TestResolve(t *testing.T) {
MethodSpecificId: subtest.identifier,
Method: subtest.method,
}
if (subtest.resolutionType == types.DIDJSONLD || subtest.resolutionType == types.JSONLD) && subtest.expectedError == "" {
if (subtest.resolutionType == "" || subtest.resolutionType == types.DIDJSONLD) && subtest.expectedError == "" {
subtest.expectedDID.Context = []string{types.DIDSchemaJSONLD}
} else {
subtest.expectedDID.Context = nil
}

resolutionResult, err := requestService.Resolve(id, types.ResolutionOption{Accept: subtest.resolutionType})
Expand Down
9 changes: 4 additions & 5 deletions tests/pytest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
FAKE_MAINNET_DID = "did:cheqd:mainnet:zFWM1mKVGGU2gHYuLAQcTJfZBebqBpGf"
FAKE_MAINNET_FRAGMENT = MAINNET_DID + "#fake_key"

DIDJSON = "application/did+json"
DIDLDJSON = "application/did+ld+json"
LDJSON = "application/ld+json"
HTML = "text/html"

IMPLICIT_TIMEOUT = 40
ENCODING = "utf-8"
Expand All @@ -30,11 +34,6 @@ def run(command, params, expected_output):
return cli


def run_interaction(cli, input_string, expected_output):
cli.sendline(input_string)
cli.expect(expected_output)


def json_loads(s_to_load: str) -> dict:
s = copy.copy(s_to_load)
s = s.replace("\\", "")
Expand Down
32 changes: 31 additions & 1 deletion tests/pytest/test_resolution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import re

import pytest
import requests

from helpers import run, TESTNET_DID, MAINNET_DID, TESTNET_FRAGMENT, MAINNET_FRAGMENT, \
FAKE_TESTNET_DID, FAKE_MAINNET_DID, FAKE_TESTNET_FRAGMENT, FAKE_MAINNET_FRAGMENT, RESOLVER_URL, PATH
FAKE_TESTNET_DID, FAKE_MAINNET_DID, FAKE_TESTNET_FRAGMENT, FAKE_MAINNET_FRAGMENT, RESOLVER_URL, PATH, \
LDJSON, DIDJSON, DIDLDJSON, HTML


@pytest.mark.parametrize(
Expand Down Expand Up @@ -29,3 +34,28 @@
)
def test_resolution(did_url, expected_output):
run("curl", RESOLVER_URL + PATH + did_url.replace("#", "%23"), expected_output)


@pytest.mark.parametrize(
"accept, expected_header, expected_body",
[
(LDJSON, LDJSON, r"(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"
r"(.*?)didResolutionMetadata(.*?)application/ld\+json"),
(DIDLDJSON, DIDLDJSON, "(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"
"(.*?)didResolutionMetadata(.*?)application/did\+ld\+json"),
("", DIDLDJSON, "(.*?)didDocument(.*?)@context(.*?)didDocumentMetadata"
"(.*?)didResolutionMetadata(.*?)application/did\+ld\+json"),
(DIDJSON, DIDJSON, r"(.*?)didDocument(.*?)(?!`@context`)(.*?)didDocumentMetadata"
r"(.*?)didResolutionMetadata(.*?)application/did\+json"),
(HTML + ",application/xhtml+xml", HTML, fr"(.*?)didDocument(.*?)(?!`@context`)(.*?)didDocumentMetadata"
fr"(.*?)didResolutionMetadata(.*?){HTML}"),
]
)
def test_resolution_content_type(accept, expected_header, expected_body):
url = RESOLVER_URL + PATH + TESTNET_DID
header = {"Accept": accept} if accept else {}

r = requests.get(url, headers=header)

assert r.headers["Content-Type"] == expected_header
assert re.match(expected_body, r.text)
1 change: 1 addition & 0 deletions types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
DIDJSON ContentType = "application/did+json"
DIDJSONLD ContentType = "application/did+ld+json"
JSONLD ContentType = "application/ld+json"
HTML ContentType = "text/html"
)

const (
Expand Down

0 comments on commit 0d98362

Please sign in to comment.