Skip to content

Commit

Permalink
feat: adds HTTPS validation and HTTP(s) tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw authored and daviddias committed May 30, 2018
1 parent 2462fdf commit 87c4202
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ Where `<FORMAT>` may be:
| `UTP` | a "utp" over `UDP` format multiaddr | `/ip4/1.2.3.4/udp/3456/utp` |
| `Websockets` | a "ws" over `TCP` or "ws" over `DNS` format multiaddr | `/ip4/1.2.3.4/tcp/3456/ws` <br> `/dnsaddr/ipfs.io/ws` |
| `WebSocketsSecure` | a "wss" over `TCP` or "wss" over `DNS` format multiaddr | `/ip6/::/tcp/0/wss` <br> `/dnsaddr/ipfs.io/wss` |
| `HTTP` | a "http" over `TCP` or `DNS` or "http" over `DNS` format multiaddr | `/ip4/127.0.0.1/tcp/90/http/baz.jpg` <br> `/dnsaddr/ipfs.io/http/baz.jpg` |
| `HTTP` | a "http" over `TCP` or `DNS` or "http" over `DNS` format multiaddr | `/ip4/127.0.0.1/tcp/90/http` <br> `/dnsaddr/ipfs.io/http` |
| `HTTPS` | a "https" over `TCP` or `DNS` or "https" over `DNS` format multiaddr | `/ip4/127.0.0.1/tcp/90/https` <br> `/dnsaddr/ipfs.io/https` |
| `WebRTCStar` | an "ipfs" over "p2p-webrtc-star" over `Websockets` or "ipfs" over "p2p-webrtc-star" over `WebSocketsSecure` format multiaddr | `/dnsaddr/ipfs.io/wss/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4` |
| `WebSocketStar` | an "ipfs" over "p2p-websocket-star" over `Websockets` or "ipfs" over "p2p-websocket-star" over `WebSocketsSecure` or "p2p-websocket-star" over `Websockets` or "p2p-websocket-star" over `WebSocketsSecure` format multiaddr | `/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star` <br> `/dnsaddr/localhost/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4` |
| `WebRTCDirect` | a "p2p-webrtc-direct" over `HTTP` format multiaddr | `/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct` |
| `Reliable` | a `WebSockets` or `WebSocketsSecure` or `HTTP` or `WebRTCStar` or `WebRTCDirect` or `TCP` or `UTP` format multiaddr | `/dnsaddr/ipfs.io/wss` |
| `WebRTCDirect` | a "p2p-webrtc-direct" over `HTTP` or "p2p-webrtc-direct" over `HTTPS` format multiaddr | `/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct` |
| `Reliable` | a `WebSockets` or `WebSocketsSecure` or `HTTP` or `HTTPS` or `WebRTCStar` or `WebRTCDirect` or `TCP` or `UTP` format multiaddr | `/dnsaddr/ipfs.io/wss` |
| `Circuit` | | `/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj` |
| `IPFS` | "ipfs" over `Reliable` or `WebRTCStar` or "ipfs" format multiaddr | `/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4` <br> `/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj` |
| | | |

Where `multiaddr` may be:

Expand Down
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ const WebSocketsSecure = or(

const HTTP = or(
and(TCP, base('http')),
and(DNS),
and(DNS, base('http'))
and(IP, base('http')),
and(DNS, base('http')),
and(DNS)
)

const HTTPS = or(
and(TCP, base('https')),
and(IP, base('https')),
and(DNS, base('https'))
)

const WebRTCStar = or(
Expand All @@ -51,12 +58,16 @@ const WebSocketStar = or(
and(WebSocketsSecure, base('p2p-websocket-star'))
)

const WebRTCDirect = and(HTTP, base('p2p-webrtc-direct'))
const WebRTCDirect = or(
and(HTTP, base('p2p-webrtc-direct')),
and(HTTPS, base('p2p-webrtc-direct'))
)

const Reliable = or(
WebSockets,
WebSocketsSecure,
HTTP,
HTTPS,
WebRTCStar,
WebRTCDirect,
TCP,
Expand Down Expand Up @@ -101,6 +112,7 @@ exports.TCP = TCP
exports.UDP = UDP
exports.UTP = UTP
exports.HTTP = HTTP
exports.HTTPS = HTTPS
exports.WebSockets = WebSockets
exports.WebSocketsSecure = WebSocketsSecure
exports.WebSocketStar = WebSocketStar
Expand Down Expand Up @@ -146,6 +158,7 @@ function and () {
}

return {
toString: function () { return '{ ' + args.join(' ') + ' }' },
input: args,
matches: matches,
partialMatch: partialMatch
Expand Down
29 changes: 29 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ describe('multiaddr validation', function () {
'/ip6/::/ip4/0.0.0.0/udp/1234/utp'
]

const goodHTTP = [
'/dnsaddr/ipfs.io',
'/dnsaddr/ipfs.io/http',
'/dnsaddr/ipfs.io/tcp/3456/http',
'/ip4/0.0.0.0/http',
'/ip4/0.0.0.0/tcp/12345/http',
'/ip6/::/http',
'/ip6/::/tcp/12345/http'
]

const goodHTTPS = [
'/dnsaddr/ipfs.io/https',
'/dnsaddr/ipfs.io/tcp/3456/https',
'/ip4/0.0.0.0/https',
'/ip4/0.0.0.0/tcp/12345/https',
'/ip6/::/https',
'/ip6/::/tcp/12345/https'
]

const goodWS = [
'/dnsaddr/ipfs.io/ws',
'/ip4/1.2.3.4/tcp/3456/ws',
Expand Down Expand Up @@ -175,6 +194,16 @@ describe('multiaddr validation', function () {
assertMismatches(mafmt.UTP, badUTP, goodIP, goodTCP, goodUDP)
})

it('HTTP validation', function () {
assertMatches(mafmt.HTTP, goodHTTP)
assertMismatches(mafmt.HTTP, goodIP, goodTCP, goodUDP)
})

it('HTTPS validation', function () {
assertMatches(mafmt.HTTPS, goodHTTPS)
assertMismatches(mafmt.HTTPS, goodHTTP, goodIP, goodTCP, goodUDP)
})

it('Reliable validation', function () {
assertMatches(mafmt.Reliable, goodUTP, goodTCP)
assertMismatches(mafmt.Reliable, goodIP, goodUDP)
Expand Down

0 comments on commit 87c4202

Please sign in to comment.