Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from valen90/feature/vapor-two
Browse files Browse the repository at this point in the history
Updated to Vapor 2
  • Loading branch information
BrettRToomey authored Apr 26, 2017
2 parents f038991 + 917f59e commit e9da69d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 52 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
os:
- linux
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode8
script:
- eval "$(curl -sL https://swift.vapor.sh/ci)"
- eval "$(curl -sL https://gist.githubusercontent.com/BrettRToomey/1256d98cce5546c696cc68d9f61ad46f/raw/d5fb266a17f26c41b59fd00867a3a2474c3efee3/TravisSwift31)"
- eval "$(curl -sL https://swift.vapor.sh/codecov)"
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import PackageDescription
let package = Package(
name: "DataURI",
dependencies: [
.Package(url: "https://github.com/vapor/core.git", majorVersion: 1),
.Package(url: "https://github.com/vapor/vapor.git", Version(2,0,0, prereleaseIdentifiers: ["beta"])),
]
)
19 changes: 1 addition & 18 deletions Sources/DataURI+String.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
import Core
import Foundation

//FIXME(Brett): I patched this into Core 1.1, remove when updated.
extension Sequence where Iterator.Element == Byte {
internal var base64Decoded: Bytes {
let bytes = [Byte](self)
let dataBase64 = Data(bytes: bytes)

guard let data = Data(base64Encoded: dataBase64) else {
return []
}

var encodedBytes = Bytes(repeating: 0, count: data.count)
data.copyBytes(to: &encodedBytes, count: data.count)

return encodedBytes
}
}

extension String {
/**
Parses a Data URI and returns its data and type.
Expand All @@ -26,6 +9,6 @@ extension String {
*/
public func dataURIDecoded() throws -> (data: Bytes, type: String) {
let (data, type, _) = try DataURIParser.parse(uri: self)
return (data, type.string)
return (data, type.makeString())
}
}
39 changes: 13 additions & 26 deletions Tests/DataURITests/DataURITests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import XCTest

import Core

@testable import DataURI
Expand All @@ -17,18 +16,18 @@ class DataURITests: XCTestCase {

func testBase64() {
//FIXME(Brett): remove when vapor/core is updated to 1.1
let base64Bytes = "SGVsbG8sIHdvcmxkIQ==".bytes
let output = base64Bytes.base64Decoded
XCTAssertEqual(output.string, "Hello, world!")
let base64Bytes: Bytes = "SGVsbG8sIHdvcmxkIQ==".makeBytes()
let output: Bytes = base64Bytes.base64URLDecoded
XCTAssertEqual(output.makeString(), "Hello, world!")
}

func testTextNoType() {
let (data, type, meta) = try! DataURIParser.parse(
uri: "data:,Hello%2C%20World!"
)

XCTAssertEqual(data.string, "Hello, World!")
XCTAssertEqual(type.string, "text/plain;charset=US-ASCII")
XCTAssertEqual(data.makeString(), "Hello, World!")
XCTAssertEqual(type.makeString(), "text/plain;charset=US-ASCII")
XCTAssertNil(meta)
}

Expand All @@ -37,18 +36,18 @@ class DataURITests: XCTestCase {
uri: "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D"
)

XCTAssertEqual(data.string, "Hello, World!")
XCTAssertEqual(type.string, "text/plain")
XCTAssertEqual(meta?.string, "base64")
XCTAssertEqual(data.makeString(), "Hello, World!")
XCTAssertEqual(type.makeString(), "text/plain")
XCTAssertEqual(meta?.makeString(), "base64")
}

func testHTMLText() {
let (data, type, meta) = try! DataURIParser.parse(
uri: "data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E"
)

XCTAssertEqual(data.string, "<h1>Hello, World!</h1>")
XCTAssertEqual(type.string, "text/html")
XCTAssertEqual(data.makeString(), "<h1>Hello, World!</h1>")
XCTAssertEqual(type.makeString(), "text/html")
XCTAssertNil(meta)
}

Expand All @@ -57,8 +56,8 @@ class DataURITests: XCTestCase {
uri: "data:text/html,<script>alert('hi');</script>"
)

XCTAssertEqual(data.string, "<script>alert('hi');</script>")
XCTAssertEqual(type.string, "text/html")
XCTAssertEqual(data.makeString(), "<script>alert('hi');</script>")
XCTAssertEqual(type.makeString(), "text/html")
XCTAssertNil(meta)
}

Expand All @@ -71,7 +70,7 @@ class DataURITests: XCTestCase {
func testPublicInterface() {
expectNoThrow() {
let (data, type) = try "data:,Hello%2C%20World!".dataURIDecoded()
XCTAssertEqual(data.string, "Hello, World!")
XCTAssertEqual(data.makeString(), "Hello, World!")
XCTAssertEqual(type, "text/plain;charset=US-ASCII")
}
}
Expand All @@ -85,16 +84,4 @@ class DataURITests: XCTestCase {
}
}
}

//FIXME(Brett): remove when Core 1.1 includes `base64Decoded`
// required for 100% coverage
func testBase64DecodeFailure() {
var bytes = "SGVsbG8sIFdvcmxkIQ%3D%3D".bytes //Hello World!
bytes.append(0x1E) //invalid control character
let decodedBytes = bytes.base64Decoded
XCTAssertEqual(
decodedBytes, [],
"Invalid character should have caused the base64Decoder to escape."
)
}
}
8 changes: 4 additions & 4 deletions Tests/DataURITests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ParserTests: XCTestCase {
var parser = DataURIParser(scanner: Scanner(bytes))
let (type, metadata) = try parser.extractType()

XCTAssertEqual(type.string, "text/html")
XCTAssertEqual(type.makeString(), "text/html")
XCTAssertNil(metadata)
}
}
Expand All @@ -66,7 +66,7 @@ class ParserTests: XCTestCase {
var parser = DataURIParser(scanner: Scanner(bytes))
let metadata = try parser.extractTypeMetadata()

XCTAssertEqual(metadata.string, "base64")
XCTAssertEqual(metadata.makeString(), "base64")
}
}

Expand All @@ -76,9 +76,9 @@ class ParserTests: XCTestCase {
var parser = DataURIParser(scanner: Scanner(bytes))
let (type, metadata) = try parser.extractType()

XCTAssertEqual(type.string, "text/html")
XCTAssertEqual(type.makeString(), "text/html")
XCTAssertNotNil(metadata)
XCTAssertEqual(metadata?.string, "base64")
XCTAssertEqual(metadata?.makeString(), "base64")
}
}

Expand Down

0 comments on commit e9da69d

Please sign in to comment.