Split SSL handling into a separate class

This makes it easier to test, as it means we don't need to test against live
APIs.
Add tests for SSL handling