'Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/217
This tests the case where the END card looks like a normal card like
\'END = \' and other similar oddities.  As long as a card starts with END
and looks like it was intended to be the END card we allow it, but with
a warning.'

def test_invalid_end_cards(self):

    horig = fits.PrimaryHDU(data=np.arange(100)).header
    def invalid_header(end, pad):
        s = horig.tostring(sep='', endcard=False, padding=False)
        s += end
        if pad:
            s += ('    ' * _pad_length(len(s)))
        if isinstance(s, bytes):
            return BytesIO(s)
        else:
            return StringIO(s)
    s = invalid_header('END    =', True)
    with catch_warnings() as w:
        h = fits.Header.fromfile(s)
        assert (h == horig)
        assert (len(w) == 1)
        assert str(w[0].message).startswith("Unexpected    bytes    trailing    END    keyword:    '    ='")
    s = invalid_header('END                    =    ', True)
    with catch_warnings() as w:
        h = fits.Header.fromfile(s)
        assert (h == horig)
        assert (len(w) == 1)
        assert str(w[0].message).startswith("Unexpected    bytes    trailing    END    keyword:    '                    ='")
    s = invalid_header('END$%&%^*%*', True)
    with catch_warnings() as w:
        h = fits.Header.fromfile(s)
        assert (h == horig)
        assert (len(w) == 1)
        assert str(w[0].message).startswith("Unexpected    bytes    trailing    END    keyword:    '$%&%^*%*'")
    s = invalid_header('END', False)
    with catch_warnings() as w:
        h = fits.Header.fromfile(s, padding=False)
        assert (h == horig)
        assert (len(w) == 1)
        assert str(w[0].message).startswith('Missing    padding    to    end    of    the    FITS    block')
