'Test header with invalid characters'

@pytest.mark.skipif('six.PY2')
def test_invalid_characters(self):

    h = fits.Header()
    h['FOO'] = 'BAR'
    h['COMMENT'] = 'hello'
    hdul = fits.PrimaryHDU(header=h, data=np.arange(5))
    hdul.writeto(self.temp('test.fits'))
    with open(self.temp('test.fits'), 'rb') as f:
        out = f.read()
    out = out.replace('hello', u'h\xe9llo'.encode('latin1'))
    out = out.replace('BAR', u'B\xc0R'.encode('latin1'))
    with open(self.temp('test2.fits'), 'wb') as f2:
        f2.write(out)
    with catch_warnings() as w:
        h = fits.getheader(self.temp('test2.fits'))
        assert (h['FOO'] == 'B?R')
        assert (h['COMMENT'] == 'h?llo')
        assert (len(w) == 1)
        assert str(w[0].message).startswith('non-ASCII    characters    are    present    in    the    FITS    file')
